python列表--查找集合中重复元素的个数
Posted 范恒
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python列表--查找集合中重复元素的个数相关的知识,希望对你有一定的参考价值。
方法一:
>>> mylist = [1,2,2,2,2,3,3,3,4,4,4,4]
>>> myset = set(mylist)
>>> for item in myset:
print("the %d has found %d" %(item,mylist.count(item)))
the 1 has found 1
the 2 has found 4
the 3 has found 3
the 4 has found 4
方法二:
>>> from collections import Counter
>>> Counter([1,2,2,2,2,3,3,3,4,4,4,4])
Counter({2: 4, 4: 4, 3: 3, 1: 1})
方法三:
>>> List=[1,2,2,2,2,3,3,3,4,4,4,4]
>>> a = {}
>>> for i in List:
if List.count(i)>1:
a[i] = List.count(i)
>>> print (a)
参考资料:
http://blog.sina.com.cn/s/blog_670445240102v8aj.html
http://www.jb51.net/article/53911.htm
以上是关于python列表--查找集合中重复元素的个数的主要内容,如果未能解决你的问题,请参考以下文章