(1)列表中的count方法(速度慢)
#嵌套列表类型的统计 l = [[1,2,3,4,5],[1,2,3,4,5],[5,6,7,8,9]] dictionary= {} s = set(l) for i in s: dict[i] = l.count(i)
(2)字典(速度慢)
l = [[1,2,3,4,5],[1,2,3,4,5],[5,6,7,8,9]]
dict = {} for i in l: if i in dict.keys(): dict[i] = int(dict[i]) +1 else: dict[i] = 1
(3)Counter(速度快)
from collections import Counter
l = [[1,2,3,4,5],[1,2,3,4,5],[5,6,7,8,9]]
cnt = Counter() for i in l:
#要将列表转为元组才可以 i = tuple(i) cnt[i] += 1
#对字典排序(倒序),变为值由大到小的列表
cnt_data=sorted(cnt.items(),key=lambda x:x[1],reverse=True)