python Counter类
from collections import Counter
这个是python内置的,Counter(iterable-or-mapping)可以对里面的各个element进行计数,返回的也是键值对,本身Counter就是字典的子类
比如
In:
words = ['red','blue','red','green','blue','blue']
Counter(words)
Out:
Counter({'blue': 3, 'red': 2, 'green': 1})
In:
Counter({'a':3,'b':5,'c':7})
Out:
Counter({'c': 7, 'b': 5, 'a': 3})
留言
張貼留言