One - One Code All

Blog Content

Python中collections.Counter()用法

Python   2016-08-29 20:26:58

collections在python官方文档中的解释是High-performance container datatypes,直接的中文翻译解释高性能容量数据类型。

它总共包含五种数据类型。


其中Counter中文意思是计数器,也就是我们常用于统计的一种数据类型,在使用Counter之后可以让我们的代码更加简单易读。


>>> from collections import Counter

>>> colors = ['red', 'blue', 'red', 'green', 'blue', 'blue']

>>> c = Counter(colors)

>>> c

Counter({'blue': 3, 'red': 2, 'green': 1})



上一篇:如何安全的卸载Python Anaconda?
下一篇:pandas的dataframe计算连续日期均线、移动平均线rolling方法

The minute you think of giving up, think of the reason why you held on so long.