问题
Is there a way to remove duplicate sublists from a list of lists, even if they are not the same ordering?
So could I do something like make:
x = [[1,2],[3,4],[5,6],[2,1],[7,8]]
into
x = [[1,2],[3,4],[5,6],[7,8]]
Is there an itertools function or something with a for loop?
Thanks!
回答1:
this will preserve the order of list and sublists, with possible duplicates in sublists:
y, s = [], set()
for t in x:
w = tuple(sorted(t))
if not w in s:
y.append(t)
s.add(w)
if
x = [[1,2],[3,4],[5,6],[2,1,1],[2,1],[7,8],[4,3],[1,2,1]]
then y
will be:
[[1, 2], [3, 4], [5, 6], [2, 1, 1], [7, 8]]
回答2:
You can use frozenset
:
>>> def remove_dups(L):
return map(list, frozenset(map(frozenset, L)))
>>> x = [[1,2],[3,4],[5,6],[2,1],[7,8]]
>>> remove_dups(x)
[[5, 6], [1, 2], [8, 7], [3, 4]]
>>>
回答3:
try this:
a=[[1,2],[3,4],[5,6],[2,1],[7,8]]
y=[]
for i in a:
if sorted(i) not in y:
y.append(i)
print y
output is
[[1, 2], [3, 4], [5, 6], [7, 8]]
来源:https://stackoverflow.com/questions/20462664/python-remove-sublists-from-list-if-same-including-not-including-order