Python列表和字典
迄今为止的代码:Python列表和字典,python,python-3.x,Python,Python 3.x,迄今为止的代码: def letterFreq(words): s = 'abcdefghijklmnopqrstuvwxyz' lst = [] for word, year_counts in words.items(): total = 0 for year_count in year_counts: total += year_count.count return word, total
def letterFreq(words):
s = 'abcdefghijklmnopqrstuvwxyz'
lst = []
for word, year_counts in words.items():
total = 0
for year_count in year_counts:
total += year_count.count
return word, total
for i in s:
if i in word:
a = i* total
b = a/
lst.append(b
字典(dict=words)看起来像这样的结构,但会随着新词的添加而改变:
{'airport':[YearCount(year=2007,count=175702),YearCount(year=2008,count=173294)],'request':[YearCount(year=2005,count=646179),YearCount(year=2006,count=677820),YearCount(year=2007,count=697645),YearCount(year=2008,count=795265)],'Loverd':[YearCount(year=2005,count=83769),YearCount(year=2006,count=87688),YearCount(year=2007,count=108634),YearCount(year=2008,count=171015)]]
我试图创建一个新的列表,按照字母表中每个字母的顺序排列,其中包含每个字母出现的频率。因此,这将是计数的总数
例如,a出现在输入数据集中的所有单词中。然后将这个数字除以所有单词中的字母总数
前
我很难堪,如果您能提供任何帮助,我将不胜感激。使用计数器获取所需分数的示例:
>>> from collections import Counter
>>> out = Counter("Hello world".lower())
>>> out
Counter({'l': 3, 'o': 2, ' ': 1, 'e': 1, 'd': 1, 'h': 1, 'r': 1, 'w': 1})
>>> [out[c] for c in 'abcdefghijklmnopqrstuvwxyz']
[0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 3, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0]
>>> freqs = [out[c] for c in 'abcdefghijklmnopqrstuvwxyz']
>>> percs = [f * 1.0 / sum(freqs) for f in freqs]
>>> percs
[0.0, 0.0, 0.0, 0.1, 0.1, 0.0, 0.0, 0.1, 0.0, 0.0, 0.0, 0.3, 0.0, 0.0, 0.2, 0.0, 0.0, 0.1, 0.0, 0.0, 0.0, 0.0, 0.1, 0.0, 0.0, 0.0]
我想这会很有用。还有,为什么“返回单词,总计”在循环中返回。当您将代码粘贴到这里时缩进是否发生了变化?我将在下一个循环中尝试使用这两个值。我以前也没用过柜台
>>> from collections import Counter
>>> out = Counter("Hello world".lower())
>>> out
Counter({'l': 3, 'o': 2, ' ': 1, 'e': 1, 'd': 1, 'h': 1, 'r': 1, 'w': 1})
>>> [out[c] for c in 'abcdefghijklmnopqrstuvwxyz']
[0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 3, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0]
>>> freqs = [out[c] for c in 'abcdefghijklmnopqrstuvwxyz']
>>> percs = [f * 1.0 / sum(freqs) for f in freqs]
>>> percs
[0.0, 0.0, 0.0, 0.1, 0.1, 0.0, 0.0, 0.1, 0.0, 0.0, 0.0, 0.3, 0.0, 0.0, 0.2, 0.0, 0.0, 0.1, 0.0, 0.0, 0.0, 0.0, 0.1, 0.0, 0.0, 0.0]