查找list中重复出现的次数【list.count】

一、count

【学习】Python List count()方法 | 菜鸟教程 (runoob.com)

count() 方法用于统计某个元素在列表中出现的次数。

list.count(obj)

以下实例展示了 count()函数的使用方法:

aList = [123, 'xyz', 'zara', 'abc', 123]

print "Count for 123 : ", aList.count(123)

print "Count for zara : ", aList.count('zara')

以上实例输出结果如下:

Count for 123 :  2
Count for zara :  1

统计重复项出现次数:

【学习】Python统计列表(List)中的重复项出现的次数并进行排序_HuaCode的博客-CSDN博客 

方法一:
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=lambda item:item[0])
print (a)

方法三:from collections import Counter
List=[1,2,3,4,5,3,2,1,4,5,6,4,2,3,4,6,2,2]
Counter(list)

方法四:(只用列表来进行实现)
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])

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值