您的位置:首页 > 编程语言 > Python开发

python列表--查找集合中重复元素的个数

2017-09-05 09:33 337 查看
方法一:

>>> 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://www.jb51.net/article/53911.htm
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: