Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从字典创建列表_Python_Python 3.x - Fatal编程技术网

Python 从字典创建列表

Python 从字典创建列表,python,python-3.x,Python,Python 3.x,字典: {'airport':[YearCount year=2007,count=175702,YearCount year=2008,count=173294],'Loverted':[YearCount year=2005,count=83769,YearCount year=2006,count=87688,YearCount year=2007,count=108634,YearCount year=2008,count=171015],'request':[YearCount yea

字典:

{'airport':[YearCount year=2007,count=175702,YearCount year=2008,count=173294],'Loverted':[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
尝试收集。计数器:

然后,您可以使用获取字母频率

total = sum(counter.values())
lst = [counter[letter] / total for letter in 'abcdefghijklmnopqrstuvwxyz']
尝试收集。计数器:

然后,您可以使用获取字母频率

total = sum(counter.values())
lst = [counter[letter] / total for letter in 'abcdefghijklmnopqrstuvwxyz']
这是一些代码的简写,如以下代码:

count = {}
for word in d:
    for letter in word:
        count[letter] = count.get(letter, 0) + 1
这是一些代码的简写,如以下代码:

count = {}
for word in d:
    for letter in word:
        count[letter] = count.get(letter, 0) + 1

您可以迭代每个字符的ASCII值。假设您已经设置了26个条目列表:

letlen = letterlength(words)
for i in range(26):
    for word in words.keys():
        lst[i]+=word.count(chr(i+ord('a'))/letlen

您可以迭代每个字符的ASCII值。假设您已经设置了26个条目列表:

letlen = letterlength(words)
for i in range(26):
    for word in words.keys():
        lst[i]+=word.count(chr(i+ord('a'))/letlen