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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM