简体   繁体   English

应用排序后从列表中提取重复项

[英]Extract Duplicates from List After Applying Sort

I am working on code snippet to extract duplicates from a list. 我正在编写代码片段以从列表中提取重复项。 I have seen several implementations/solutions on this site. 我在这个网站上看到了几个实现/解决方案。 However, I am not getting this line correctly - syntax wise I think. 但是,我没有正确地获得这一行 - 我认为语法明智。 After sorting, compare index(x) with index(x+1). 排序后,将index(x)与index(x + 1)进行比较。 If it is add to the set. 如果它被添加到集合中。

print(set([i for i in a if (a[i] == a[i+1]))

a = [1,2,3,2,1,5,6,5,5,5]
print(a)
print(set(sorted(a)))
# l1[i] == l1[i+1]
print(set([i for i in a if (a[i] == a[i+1]))
print(set([i for i in a if sum([1 for item in a if item == i]) > 1]))

Expected results: {1, 2, 5} 预期成果:{1,2,5}

you could use collections.Counter : 你可以使用collections.Counter

from collections import Counter

a = [1,2,3,2,1,5,6,5,5,5]
c = Counter(a)

res = [n for n, m in c.items() if m > 1]
print(res)  # [1, 2, 5]

this way you iterate once over the list and once over the counter only. 这样你就可以在列表上迭代一次,只在柜台上迭代一次。

From what I could gather you are trying to implement this logic, this code runs in O(nlogn) time complexity while the code which runs with counter runs in O(n) time complexity means it is faster and cleaner. 从我可以收集到的是你正在尝试实现这个逻辑,这个代码以O(nlogn)时间复杂度运行,而运行计数器的代码以O(n)时间复杂度运行意味着它更快更干净。

a = [1,2,3,2,1,5,6,5,5,5]
a.sort()
print(set([a[i] for i in range(len(a)-1) if (a[i] == a[i+1])]) )

OUTPUT OUTPUT

set([1, 2, 5])

How about this instead? 相反怎么样?

a = [1,2,3,2,1,5,6,5,5,5]
duplicates = set(element for element in a if a.count(element) > 1)
print(duplicates)

Output: 输出:

{1, 2, 5}

Suggest a simple solution to find duplicates from List. 建议一个简单的解决方案,从列表中查找重复项。

>>> a = [1,2,3,2,1,5,6,5,5,5]
>>> a.sort()
>>> set([x for x in a if a.count(x) > 1])

Output: {1, 2, 5} 输出: {1, 2, 5}

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

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