Python3如何实现查找列表中重复元素个数的三种高效方法?
- 内容介绍
- 文章标签
- 相关推荐
本文共计224个文字,预计阅读时间需要1分钟。
方法一:使用集合pythonmylist=[1, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4]myset=set(mylist)for item in myset: print(fthe {item} has found {mylist.count(item)})
方法二:使用collections模块pythonfrom collections import Counter
mylist=[1, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4]counter=Counter(mylist)for item, count in counter.items(): print(fthe {item} has found {count})
方法一:
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查找列表中重复元素的个数方法文章请查看下面的相关链接
本文共计224个文字,预计阅读时间需要1分钟。
方法一:使用集合pythonmylist=[1, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4]myset=set(mylist)for item in myset: print(fthe {item} has found {mylist.count(item)})
方法二:使用collections模块pythonfrom collections import Counter
mylist=[1, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4]counter=Counter(mylist)for item, count in counter.items(): print(fthe {item} has found {count})
方法一:
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查找列表中重复元素的个数方法文章请查看下面的相关链接

