Python并删除列表列表中的重复项,而不管列表中的顺序如何 [英] Python and remove duplicates in list of lists regardless of order within lists
问题描述
我进行了搜索,但还没有找到与我相同的问题.我想从python中的列表列表中删除重复项;但是,我不在乎值在列表中的顺序.目前我这样做的方式太耗时了.
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.
我想做什么:
A = [[1,2,3] , [2,3,4] , [3,4,5] , [3,2,4]]
我想搜索A并删除所有重复项.这里的重复项将是[2,3,4]和[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]]
我目前的情况:
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!
推荐答案
冻结集非常适合需要嵌套集的此类情况:
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.)
如果您确实需要保留订单,则可以遍历A
,同时跟踪到目前为止看到的冻结集:
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]]
(这不是经过优化的;理想情况下,您只需调用一次frozenset(x)
并将结果存储在变量中.)
(This isn't optimized; ideally, you'd only call frozenset(x)
once and store the result in a variable.)
这篇关于Python并删除列表列表中的重复项,而不管列表中的顺序如何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!