获取在列表中出现多次的唯一项目列表 [英] Get a unique list of items that occur more than once in a list
本文介绍了获取在列表中出现多次的唯一项目列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个项目清单:
mylist = ['A','A','B','C','D','E','D']
我想返回一个在 mylist 中多次出现的项目的唯一列表,以便我想要的输出是:
I want to return a unique list of items that appear more than once in mylist, so that my desired output would be:
[A,D]
不确定如何做到这一点,但我的流程是首先附加每个项目的计数,然后删除等于 1 的任何内容.然后进行重复数据删除,但这似乎是一种非常迂回、低效的方法,所以我正在寻求建议.
Not sure how to even being this, but my though process is to first append a count of each item, then remove anything equal to 1. Then dedupe, but this seems like a really roundabout, inefficient way to do it, so I am looking for advice.
推荐答案
您可以使用 collections.Counter
可以轻松完成您所描述的操作:
You can use collections.Counter
to do what you have described easily:
from collections import Counter
mylist = ['A','A','B','C','D','E','D']
cnt = Counter(mylist)
print [k for k, v in cnt.iteritems() if v > 1]
# ['A', 'D']
这篇关于获取在列表中出现多次的唯一项目列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文