我正在使用以下代码获取文本中字母的频率:
for s in 'abcdefghijklmnopqrstuvwxyz ':
count = 0
for char in rawpunct.lower():
if s == char:
count +=1
result = s, '%.3f' % (count*100/len(rawpunct.lower()))
f_list.append(result)
结果是:
['0.061', '0.012', '0.017', '0.030', '0.093', '0.016', '0.016',
'0.049', '0.050', '0.001', '0.006', '0.034', '0.018', '0.052', '0.055',
'0.013', '0.001', '0.041', '0.050', '0.069', '0.021', '0.007', '0.017',
'0.001', '0.013', '0.000', '0.159']
但我想存储累积频率,即创建此列表:
['0.061', '0.073', '0.100', '0.130' ............ ]
有人知道怎么做吗?
请您参考如下方法:
你可以使用import numpy 然后将结果设为数组 results=numpy.array(result) 最后是 'f_list=numpy.cumsum(results)'
