collections.Counter 计数器 elements()

Counter是一个简单的计数器,例如,统计字符出现的个数

s='abcdefg'

collections.Counter(s)

Counter({'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1, 'f': 1, 'g': 1})


elements()返回一个迭代器。元素被重复了多少次,在该迭代器中就包含多少个该元素。元素排列无确定顺序,个数小于1的元素不被包含。

collections.Counter(s).elements()

<itertools.chain at 0x247e62607b8>

list(collections.Counter(s).elements())
['a', 'b', 'c', 'd', 'e', 'f', 'g']

猜你喜欢

转载自blog.csdn.net/ustbclearwang/article/details/80668006