Python 从频率创建报告
如何将频率字典转换为报表字典 从<代码>[('a':2),('b':6),('cd':1),('g':9)] 到 我用这个来创建频率字典Python 从频率创建报告,python,Python,如何将频率字典转换为报表字典 从[('a':2),('b':6),('cd':1),('g':9)] 到 我用这个来创建频率字典 openfile = open(filename, 'r') x = openfile.read() uplist = p.split() wc = {} for word in uplist: if word in wc.keys(): wc[word] +=1 else: wc[word] = 1 return w
openfile = open(filename, 'r')
x = openfile.read()
uplist = p.split()
wc = {}
for word in uplist:
if word in wc.keys():
wc[word] +=1
else:
wc[word] = 1
return wc.items()
现在,我必须创建一个报告函数。只需循环并打印:
for item, freq in items:
print(item, freq)
严格来说,因为您返回的是
wc.items()
,所以您没有字典,而是元组列表。您可以使用wc=collections.Counter(uplist)
for item, freq in items:
print(item, freq)