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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

猪笨是念来过倒   2021-04-21 我要评论

方法一:

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查找列表中重复元素的个数方法文章请查看下面的相关链接

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们