Python中对序列数据的汇总(collections)

目录

Counter

most_common 


对于序列如字符串str、列表list和tuple可以统计里面数据出现的次数。我们使用的是 collections 模块。

collections模块的常用方法有:

  • 计数器(Counter)
  • 双向队列(deque)
  • 默认字典(defaultdict)
  • 有序字典(OrderedDict)
  • 可命名元组(namedtuple)

使用以上类型时需要导入模块 from collections import *

Counter

Counter()方法对传入的序列中出现的数据进行汇总,返回一个<class 'collections.Counter'>的对象

from collections import Counter
a="aabcac"
b=['a','a','b','c','a','c']
c=('a','a','b','c','a','c')
print(Counter(a),type(Counter(a)))
print(Counter(b))
print(Counter(c))
#######################################
Counter({'a': 3, 'c': 2, 'b': 1}) <class 'collections.Counter'>
Counter({'a': 3, 'c': 2, 'b': 1})
Counter({'a': 3, 'c': 2, 'b': 1})

most_common 

most_common方法对Counter()汇总的数据进行从高到低的排序,返回前 n 个元素的字典,返回的是列表型的数据

from collections import Counter
a="aabcac"
b=['a','a','b','c','a','c']
c=('a','a','b','c','a','c')
print(Counter(a))
print(Counter(b))
print(Counter(c))

print(Counter(a).most_common(3),type(Counter(a).most_common(3)))
print(Counter(b).most_common(2))
print(Counter(c).most_common(1))
##############################################
Counter({'a': 3, 'c': 2, 'b': 1})
Counter({'a': 3, 'c': 2, 'b': 1})
Counter({'a': 3, 'c': 2, 'b': 1})
[('a', 3), ('c', 2), ('b', 1)] <class 'list'>
[('a', 3), ('c', 2)]
[('a', 3)]

猜你喜欢

转载自blog.csdn.net/qq_36119192/article/details/83660699
今日推荐