python collections counter的值怎么取出来 怎么调用python中的collections
python counter\u8ba1\u6570\u540e\u600e\u4e48\u4ece\u5927\u5230\u5c0f\u8fd4\u56de\u503c\u8fd9\u79cd\u8bf4\u6cd5\u4e0d\u51c6\u786e\uff0c\u786e\u5207\u7684\u8bdd\uff0c\u662f\u4e0d\u80fd\u8fd4\u56de\u591a\u4e2a\u503c\u7684 \u4f46\u662f\uff0c\u5982\u4e0b\u8fd9\u79cd\u60c5\u51b5 def abs(x, y, z): return x, y, z # \u548c return (x, y, z) \u4e00\u6837 \u770b\u4f3c\u662f\u8fd4\u56de\u4e86\u591a\u4e2a\u503c\uff0c\u5176\u5b9e\u662f\u8fd4\u56de\u4e86\u4e00\u4e2a\u5143\u7ec4 \u5f53\u7136\uff0c\u4e5f\u53ef\u4ee5\u8fd4\u56de\u5217\u8868\uff0c\u5b57\u5178\u7b49
\u4ee3\u7801\u5982\u4e0b:
# -*- coding: utf-8 -*-
"""
\u6bd4\u5982\u6211\u4eec\u7528\u6237\u62e5\u6709\u4e00\u4e2a\u8fd9\u6837\u7684\u6570\u636e\u7ed3\u6784\uff0c\u6bcf\u4e00\u4e2a\u5bf9\u8c61\u662f\u62e5\u6709\u4e09\u4e2a\u5143\u7d20\u7684tuple\u3002
\u4f7f\u7528namedtuple\u65b9\u6cd5\u5c31\u53ef\u4ee5\u65b9\u4fbf\u7684\u901a\u8fc7tuple\u6765\u751f\u6210\u53ef\u8bfb\u6027\u66f4\u9ad8\u4e5f\u66f4\u597d\u7528\u7684\u6570\u636e\u7ed3\u6784\u3002
"""
from collections import namedtuple
websites = [
('Sohu', 'http://www.google.com/', u'\u5f20\u671d\u9633'),
('Sina', 'http://www.sina.com.cn/', u'\u738b\u5fd7\u4e1c'),
('163', 'http://www.163.com/', u'\u4e01\u78ca')
]
Website = namedtuple('Website', ['name', 'url', 'founder'])
for website in websites:
website = Website._make(website)
print website
# Result:
Website(name='Sohu', url='http://www.google.com/', founder=u'\u5f20\u671d\u9633')
Website(name='Sina', url='http://www.sina.com.cn/', founder=u'\u738b\u5fd7\u4e1c')
Website(name='163', url='http://www.163.com/', founder=u'\u4e01\u78ca')
使用counter类的items()函数即可,Python3程序如下:
for each in count.items():#假设存在counter类的count
(a,b)=each
print(a,b.sep='',end='
',file='./test.txt')
最为简单的方法是利用表理解,生成一个新的字典必须要保证键值是一一对应的d={'one':1,'two':2,'three':3,'four':4}di={v:kfork,vind.items()}di[1]
这还不简单、、、通过键来访问值啊。。
s1 = ['zh','like','like','di','ag']
c2 = Counter(s1)
print(c2)
print(c2['zh'])
扩展阅读:python console log ... python tkinter scale ... python手机版下载官方 ... python初学编程必背 ... python colormode ... 学python后到底能干什么 ... run with python console ... python try finally ... python scrollbar ...