如果一个Python list中有很多重复的项,如何有效地找到多少重复的项呢?
可以使用collection的Counter方法。。
>>> from collections import Counter
>>> Counter([11,22,11,44,22,33])
Counter({11: 2, 22: 2, 33: 1, 44: 1})
>>> print Counter(l)[33]
1
作者:Shane
出处:http://www.cnblogs.com/bluescorpio/p/3799682.html
本文介绍了一种高效的方法来统计Python列表中重复项的数量,使用collections模块的Counter方法,轻松实现元素频率统计。
5万+

被折叠的 条评论
为什么被折叠?



