本文介绍了如何仅打印python列表中的重复元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有任何内置方法可以打印 python 列表中存在的重复元素.
Is there any inbuilt way to print duplicate elements present in a python list.
我可以为此编写程序.
我正在寻找的是是否有任何内置方法或相同的东西.
All I'm searching for is if there is any inbuilt method or something for the same.
例如:
对于输入[4,3,2,4,5,6,4,7,6,8]
我需要操作 4,6
推荐答案
可以解决这个问题
There is the Counter
class from collections
that does the trick
from collections import Counter
lst = [4,3,2,4,5,6,4,7,6,8]
d = Counter(lst) # -> Counter({4: 3, 6: 2, 3: 1, 2: 1, 5: 1, 7: 1, 8: 1})
res = [k for k, v in d.items() if v > 1]
print(res)
# [4, 6]
这篇关于如何仅打印python列表中的重复元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!