List = [1,2,3,4,5,3,2,1,4,5,6,4,2,3,4,6,2,2]List_set =set(List)#List_set是另外一个列表,里面的内容是List里面的无重复 项for item in List_set:print("the %d has found %d"%(item,List.count(item)))
方法二:(利用字典的特性来实现)
List=[1,2,3,4,5,3,2,1,4,5,6,4,2,3,4,6,2,2]a ={}for i in List:if List.count(i)>1: a[i]= List.count(i) a =sorted(a.items(), key=lambdaitem:item[0])print (a)
方法三:内置函数
from collections import CounterList=[1,2,3,4,5,3,2,1,4,5,6,4,2,3,4,6,2,2]c =Counter(list)c.values()sum(c.values())c.keys()c.clear()list(c)set(c)dict(c)c.items()c +=Counter()#这个是最神奇的,就是可以将负数和0的值对应的key项去掉
方法四:(只用列表来进行实现)
List=[1,2,3,4,5,3,2,1,4,5,6,4,2,3,4,6,2,2]count_times = []for i in l : count_times.append(l.count(i))m =max(count_times)n = l.index(m)print (list[n])