在Python字典中动态创建键和值

1 投票
2 回答
2655 浏览
提问于 2025-04-20 09:39

我现在遇到的问题是,我想读取一个文件,这个文件里面有一堆单词的列表。然后我需要计算每个单词里面有多少个元音字母,并把每个单词、它的元音字母数量以及这个单词的总元音字母数放在一个表格里,最后还要显示所有单词的元音字母总数。

我打算通过一个循环来读取这个文件,并为每个单词创建一个字典,像这样:

mississippi['a_count' : 0, 'e_ocunt' : 0, 'i_count' : 4 ,'o_count' : 0, 'u_count' : 0, 'y_count' : 0]

但我现在的问题是,我不太确定在循环中如何创建这些字典,因为变量会随着循环而变化。我最终得到的字典都是空的。

这是我输出结果的截图 https://i.stack.imgur.com/4yusw.jpg

我在文件里的测试代码是“Mississippi California Wisconsin”,每个单词都在不同的行上。

try:
    word_file = open("vowel.txt", "r")

    count = 0
    dic = {}

    a_count = 0
    e_count = 0
    i_count = 0
    o_count = 0
    u_count = 0
    y_count = 0
    total_count = 0

    #this establishes the top of the table
    print('Number','{:>8}'.format('word'),'{:>8}'.format('A'),'{:>4}'.format('E'),'{:>4}'.format('I'),'{:>4}'.format('O'),'{:>4}'.format('U'),'{:>4}'.format('Y'),'{:>8}'.format('Total'))
    print("__________________________________________________________")


    for word in word_file:
        count+=1
        word = {}
        print(word)

        word_a_count = 0
        word_e_count = 0
        word_i_count = 0
        word_o_count = 0
        word_u_count = 0
        word_y_count = 0
        word_total_count = 0

        for letters in word:
            print(letters)
            if letters.lower() == "a":
                a_count+= 1
                total_count += 1
                word_a_count +=1
                word['a_count'] = word_a_count
            if letters.lower() == "e":
                e_count+= 1
                total_count += 1
                word_e_count +=1
                word['e_count'] = word_e_count
            if letters.lower() == "i":
                i_count+= 1
                total_count += 1
                word_i_count +=1
                word['i_count'] = word_i_count
            if letters.lower() == "o":
                o_count+= 1
                total_count += 1
                word_o_count +=1
                word['o_count'] = word_o_count
            if letters.lower() == "u":
                u_count+= 1
                total_count += 1
                word_u_count +=1
                word['u_count'] = word_u_count
            if letters.lower() == "y":
                y_count+= 1
                total_count += 1
                word_y_count +=1
                word['y_count'] = word_y_count

            print('Totals','{:>8}'.format('    '),'{:>8}'.format(word['a_count']),'{:>4}'.format\
            (word['e_count']),'{:>4}'.format(word['i_count']),'{:>4}'.format\
            (word['o_count']),'{:>4}'.format(word['u_count']),'{:>4}'.\
            format(word['y_count']))

    #this creates the bottom barrier of the table
    print("__________________________________________________________")

    #code for totals print
    print('Totals','{:>8}'.format('    '),'{:>8}'.format(a_count),'{:>4}'.format(e_count),'{:>4}'.format(i_count),'{:>4}'.format(o_count),'{:>4}'.format(u_count),'{:>4}'.format(y_count),'{:>6}'.format(total_count))

except IOError:
    print("The file does not seem to exists. The program is halting.")

2 个回答

1

在Python 2中,我会这样做...

#! /usr/bin/env python

'''
Count vowels in a list of words & show a grand total

Words come from a plain text file with one word per line
'''

import sys

vowels = 'aeiouy'


def make_count_dict():
    ''' Create a dict for counting vowels with all values initialised to 0 '''
    return dict(zip(vowels, (0,)*len(vowels)))


def get_counts(d):
    return ' '.join('%2d' % d[k] for k in vowels) 


def count_vowels(wordlist):
    hline = '_'*45
    print '%3s: %-20s: %s' % ('Num', 'Word', ' '.join('%2s' % v for v in vowels))
    print hline

    total_counts = make_count_dict()
    for num, word in enumerate(wordlist, start=1):
        word_counts = make_count_dict()
        for ch in word.lower():
            if ch in vowels:
                word_counts[ch] += 1
                total_counts[ch] += 1
        print '%3d: %-20s: %s' % (num, word, get_counts(word_counts))

    print hline
    print '%-25s: %s' % ('Total', get_counts(total_counts)) 


def main():
    fname = len(sys.argv) > 1 and sys.argv[1]
    if fname:
        try:
            with open(fname, 'r') as f:
                wordlist = f.read().splitlines()
        except IOError:
            print "Can't find file '%s'; aborting." % fname
            exit(1)
    else:
        wordlist = ['Mississippi', 'California', 'Wisconsin']

    count_vowels(wordlist)


if __name__ == '__main__':
    main()
3

重点关注这一部分——在每次循环中,word都会被重新赋值为空字典:

for word in word_file:
        count+=1
        word = {}

不过,如果把 word = {} 这一行注释掉,当从文件中读取到第一个元音字母时就会报错(因为这时字典不再是空的)。记住,word 是你正在遍历的文本文件中的当前行,所以 word['u_count'] = word_u_count 被理解为要改变字符串中的一个字符。Python中的字符串是不可变的,所以会报错。

你的程序比需要的要长得多——当你发现代码中有重复的部分时,可以考虑重构一下,利用循环和迭代来让程序更简洁。你可以把计算一个单词中字母数量的所有逻辑放到一个过程里:

def countletters(word, letterstocount):

    count = {}
    word = word.lower()

    for char in word:       
        if char in letterstocount:  
            if char in count:
                count[char] += 1
            else:
                count[char] = 1
    return count

#example call
vowels = "aeiou"
print(countletters('Arizona', vowels))

然后你可以对文件中的每个单词调用这个过程。

撰写回答