简体   繁体   English

删除列表列表中的所有重复项(不包括原始列表)

[英]Remove all duplicates in a list of list (not including the original)

I have a list of lists and I want to remove all the duplicates so that the similar lists will not appear at all in the new list.我有一个列表列表,我想删除所有重复项,这样类似的列表就不会出现在新列表中。

k = [[1, 2], [4], [5, 6, 2], [1, 2], [3], [4]]

output == [[5,6,2], [3]]

So for example, [1,2] have a duplicate so it should not appear in the final list at all.例如, [1,2]有一个重复项,所以它根本不应该出现在最终列表中。 This is different from what others have asked as they wanted to keep one copy of the duplicate in the final list.这与其他人要求的不同,因为他们希望在最终列表中保留一份副本。

You can use count to count the number of occurrences of a element.您可以使用count来计算元素出现的次数。

Approach 1 Time complexity O(n^2)方法 1 时间复杂度O(n^2)

final=[]
for i in k:
    if k.count(i)==1:
        final.append(i)
print(final)

[[5, 6, 2], [3]]

Pythonic way to write this is : Pythonic 的写法是:

final=[i for i in k if k.count(i)==1]

Approach 2 Time complexity O(n^2)方法 2 时间复杂度O(n^2)

Or you can search if ith element is present in rest of the list or not.或者您可以搜索列表的其余部分是否存在第 i 个元素。 If present don't add it to final .如果存在,请不要将其添加到final

for i,lst in enumerate(k):
    if lst not in k[:i]+k[i+1:]:
        final.append(lst)
print(final)

output输出

[[5, 6, 2], [3]]

pythonic way to write this:写这个的pythonic方式:

final=[i for i,lst in enumerate(k) if lst not in k[:i]+k[i+1:]]

Approach 3 Time complexity is O(n)方法 3 时间复杂度为O(n)

You can achieve this in O(n) by using dictionaries.您可以使用字典在O(n)实现这一点。

dic={}
k=list(map(tuple,k))   #Since key values in dictionary should always be immutable.
for i in k:
    dic[i]=dic.setdefault(i,0)+1

final=[]

for k in dic:
    if dic[k]== 1:
        final.append(list(k))

For the general problem of dropping duplicates, you could use collections.Counter , although it requires hashable values.对于删除重复项的一般问题,您可以使用collections.Counter ,尽管它需要可哈希值。

import collections

def drop_all_duplicates(values):
    # As of CPython 3.7, Counter remembers insertion order.
    value_counts = collections.Counter(values)
    return (value for value, count in value_counts.items() if count == 1)

>>> list(drop_all_duplicates([1, 1, 2, 3, 3, 4, 5, 5, 6])
[2, 4, 6]

This could be expanded to cover non-hashable values by accepting a function that converts them to hashable (eg a function that converts lists to tuples).这可以通过接受将它们转换为可散列的函数(例如,将列表转换为元组的函数)来扩展以涵盖不可散列的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM