Python and remove duplicates in list of lists rega

2019-02-25 18:49发布

I have searched and haven't quite found the same question as mine. I want to remove duplicates from a list of lists in python; however, I don't care what order the values are in the list. They way I am doing it currently is too time-consuming.

What I want to do:

A = [[1,2,3] , [2,3,4] , [3,4,5] , [3,2,4]]

I want to search through A and remove all duplicates. The duplicates here would be [2,3,4] and [3,2,4]. This would reduce down to:

smaller_A = [[1,2,3] , [2,3,4], [3,4,5]]

How I am currently doing it:

todelete = []
for i in range(len(A)):
    for j in range(i+1,len(A)):
        if set(A[i]) == set(A[j]):
           todelete.append(j)

todelete = sorted(set(todelete))

smaller_A= [A[i] for i in range(len(A)) if i not in todelete]

Again, this works, but it is very time consuming when my lists are large. Any ideas? Thanks!

2条回答
乱世女痞
2楼-- · 2019-02-25 19:04

Frozensets are perfect for cases like this, when you need to nest sets:

>>> A = [[1,2,3], [2,3,4], [3,4,5], [3,2,4]]
>>> smaller_A = {frozenset(x) for x in A}
>>> smaller_A
{frozenset({1, 2, 3}), frozenset({2, 3, 4}), frozenset({3, 4, 5})}

To convert back to lists, you can do this:

>>> [list(x) for x in smaller_A]
[[1, 2, 3], [2, 3, 4], [3, 4, 5]]

This won't conserve the order of your lists or the elements inside them. (Although it didn't make a difference here.)

If you do need to preserve order, you can iterate over A while keeping track of frozensets seen so far:

>>> A = [[1,2,3], [2,3,4], [3,4,5], [3,2,4]]
>>> seen = set()
>>> smaller_A = []
>>> for x in A:
...     if frozenset(x) not in seen:
...         smaller_A.append(x)
...         seen.add(frozenset(x))
...
>>> smaller_A
[[1, 2, 3], [2, 3, 4], [3, 4, 5]]

(This isn't optimized; ideally, you'd only call frozenset(x) once and store the result in a variable.)

查看更多
萌系小妹纸
3楼-- · 2019-02-25 19:05

you can do a trick with sorting this way

for i in range(len(A)): A[i].sort()

then remove duplicates

查看更多
登录 后发表回答