python - Removing repeated sub-lists from a list -
i have list follows:
l = [['a', 'c', 'd'], ['b', 'e'], ['a', 'c', 'd'], ['a', 'c', 'd'], ['b', 'e'], ['f']]
the result should be:
[['a', 'c', 'd'], ['b', 'e'], ['f']]
the order of elements not important.
i tried as:
print list(set(l))
does numpy has better way
lists not "hashable" type , cannot members of set.
frozen sets can, first convert (also making sublists order-insentive), , later convert lists.
print map(list, set(map(frozenset, l)))
or if prefer comprehensions,
print [list(x) x in {frozenset(x) x in l}]
i doubt numpy offers "better" (for definition of better) way.
this way imo clearest , pythonic.
the reason lists cannot part of sets mutable, hash different hash after changed; being in hash-based set make confusing behavior.
Comments
Post a Comment