词典:
{'airport':[YearCount(year = 2007,count = 175702),YearCount(year = 2008,count = 173294)],'wandered':[YearCount(year = 2005,count = 83769),YearCount( year = 2006,count = 87688),YearCount(year = 2007,count = 108634),YearCount(year = 2008,count = 171015)],'request':[YearCount(year = 2005,count = 646179),YearCount( year = 2006,count = 677820),YearCount(year = 2007,count = 697645),YearCount(year = 2008,count = 795265)]}
这会计算字典键中的总字母数:
def letterlength(words):
length = 0
for word in words.keys():
length += len(word)
return length
我试图用这个函数创建一个列表,但我没有得到一个列表。它应该返回单词中字母的字母频率。我知道这很冗长,但我找不到更简单的方法:
def letterFreq(words):
lst = []
a = 0
b = 0
c = 0
d=0
e=0
f=0
g=0
h=0
i=0
j=0
k=0
l=0
m=0
n=0
o=0
p=0
q=0
r=0
s=0
t=0
u=0
v=0
w=0
x=0
y=0
z=0
for word in words.keys():
a += word.count('a')
b += word.count('b')
c += word.count('c')
d += word.count('d')
e += word.count('e')
f += word.count('f')
g += word.count('g')
h += word.count('h')
i += word.count('i')
j += word.count('j')
k += word.count('k')
l += word.count('l')
m += word.count('m')
n += word.count('n')
o += word.count('o')
p += word.count('p')
q += word.count('q')
r += word.count('r')
s += word.count('s')
t += word.count('t')
u += word.count('u')
v += word.count('v')
w += word.count('w')
x += word.count('x')
y += word.count('y')
z += word.count('z')
return (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z)
lst.append(a/letterlength(words))
lst.append(b/letterlength(words))
lst.append(c/letterlength(words))
lst.append(d/letterlength(words))
lst.append(e/letterlength(words))
lst.append(f/letterlength(words))
lst.append(g/letterlength(words))
lst.append(h/letterlength(words))
lst.append(i/letterlength(words))
lst.append(j/letterlength(words))
lst.append(k/letterlength(words))
lst.append(l/letterlength(words))
lst.append(m/letterlength(words))
lst.append(n/letterlength(words))
lst.append(o/letterlength(words))
lst.append(p/letterlength(words))
lst.append(q/letterlength(words))
lst.append(r/letterlength(words))
lst.append(s/letterlength(words))
lst.append(t/letterlength(words))
lst.append(u/letterlength(words))
lst.append(v/letterlength(words))
lst.append(w/letterlength(words))
lst.append(x/letterlength(words))
lst.append(y/letterlength(words))
lst.append(z/letterlength(words))
return lst
答案 0 :(得分:3)
collections.Counter(itertools.chain(*d))
这是以下某些代码的简写:
count = {}
for word in d:
for letter in word:
count[letter] = count.get(letter, 0) + 1
答案 1 :(得分:2)
尝试collections.Counter
:
import collections
counter = collections.Counter()
for word in words:
counter.update(word)
然后您可以使用
获取字母频率total = sum(counter.values())
lst = [counter[letter] / total for letter in 'abcdefghijklmnopqrstuvwxyz']
答案 2 :(得分:0)
您可以迭代每个字符的ASCII值。假设您已经设置了26个条目列表:
letlen = letterlength(words)
for i in range(26):
for word in words.keys():
lst[i]+=word.count(chr(i+ord('a'))/letlen