代码点读机 / Python / collections

collections Python · 常用模块

提供额外的高阶容器类型,如计数器、默认字典、双端队列等。

怎么用

from collections import Counter

小例子

from collections import Counter
print(Counter('aab'))

运行结果

Counter({'a': 2, 'b': 1})

提醒:适合计数、分组和队列场景。

官方文档:https://docs.python.org/zh-cn/3/library/collections.html

在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← Python全部词条