python collections counter的值怎么取出来

如题所述

使用counter类的items()函数即可,Python3程序如下:

for each in count.items():#假设存在counter类的count
    (a,b)=each
    print(a,b.sep='\t',end='\n',file='./test.txt')

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-03-09
最为简单的方法是利用表理解,生成一个新的字典必须要保证键值是一一对应的d={'one':1,'two':2,'three':3,'four':4}di={v:kfork,vind.items()}di[1]
第2个回答  2019-02-26
这还不简单、、、通过键来访问值啊。。
s1 = ['zh','like','like','di','ag']
c2 = Counter(s1)
print(c2)
print(c2['zh'])