码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-11-21 08:45:19      阅读:435      评论:0      收藏:0      [点我收藏+]

标签:import   查找   print   集合   方法   art   blog   重复元素   item   

方法一:

>>> 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列表--查找集合中重复元素的个数

标签:import   查找   print   集合   方法   art   blog   重复元素   item   

原文地址:http://www.cnblogs.com/AaronFan/p/6084175.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!