Python3查找列表中重复元素的个数的3种方法详解

方法一:

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)

更多关于Python查找列表中重复元素的个数方法文章请查看下面的相关链接

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。