python - 我可以简化我的代码,这样我就不会单独写出字母表中的每个字母吗?

标签 python dictionary

我正在编写一个循环遍历葛底斯堡演说并计算每个字母出现次数的代码。然后该字母作为键存储在字典中,每个键的值是该特定值出现的总次数。葛底斯堡演说分为三行供我们循环播放。我写的方式是循环遍历每一行,但我无法添加行的出现次数以便在字典中得到总数。例如,如果在第 1 行中有 5 个 A,在第 2 行中有 10 个 A,在第 3 行中有 15 个 A,则总数应为 30 个 A,字典应为 a:30。

另外,在给定的文件中,第 1 行和第 2 行之间以及第 2 行和第 3 行之间有空行,我不知道如何删除这些行以用于我的循环。

最后,现在我已经为该程序写下了每封信,但我想知道是否有更简单的方法可以简化我所拥有的。

# Function: readFile
# Parameters: filename
# Return: dictionary
# Detail: Loop through each line of the Gettysburg Address File and count the occurrences of each letter in each line
# Detail: Sum the occurrences of each letter for each line to find the total occurences of each letter for the entire document
# Add the letter and its occurence to a dictionary key:value = letter:occurence
def readFile(filename = "gettysburg.txt"):
    fileIn = open(filename, "r")
    dictionary = {}
    for line in fileIn:
        line.lower()
        letter = "a"
        aCount = line.count("a")
        dictionary[letter] = aCount
        letter = "b"
        bCount = line.count("b")
        dictionary[letter] = bCount
        letter = "c"
        cCount = line.count("c")
        dictionary[letter] = cCount
        letter = "d"
        dCount = line.count("d")
        dictionary[letter] = dCount
        letter = "e"
        eCount = line.count("e")
        dictionary[letter] = eCount
        letter = "f"
        fCount = line.count("f")
        dictionary[letter] = fCount
        letter = "g"
        gCount = line.count("g")
        dictionary[letter] = gCount
        letter = "h"
        hCount = line.count("h")
        dictionary[letter] = hCount
        letter = "i"
        iCount = line.count("i")
        dictionary[letter] = iCount
        letter = "j"
        jCount = line.count("j")
        dictionary[letter] = jCount
        letter = "k"
        kCount = line.count("k")
        dictionary[letter] = kCount
        letter = "l"
        lCount = line.count("l")
        dictionary[letter] = lCount
        letter = "m"
        mCount = line.count("m")
        dictionary[letter] = mCount
        letter = "n"
        nCount = line.count("n")
        dictionary[letter] = nCount
        letter = "o"
        oCount = line.count("o")
        dictionary[letter] = oCount
        letter = "p"
        pCount = line.count("p")
        dictionary[letter] = pCount
        letter = "q"
        qCount = line.count("q")
        dictionary[letter] = qCount
        letter = "r"
        rCount = line.count("r")
        dictionary[letter] = rCount
        letter= "s"
        sCount = line.count("s")
        dictionary[letter] = sCount
        letter = "t"
        tCount = line.count("t")
        dictionary[letter] = tCount
        letter = "u"
        uCount = line.count("u")
        dictionary[letter] = uCount
        letter = "v"
        vCount = line.count("v")
        dictionary[letter] = vCount
        letter = "w"
        wCount = line.count("w")
        dictionary[letter] = wCount
        letter = "x"
        xCount = line.count("x")
        dictionary[letter] = xCount
        letter = "y"
        yCount = line.count("y")
        dictionary[letter] = yCount
        letter = "z"
        zCount = line.count("z")
        dictionary[letter] = bCount
        print(dictionary)

    fileIn.close()

# function: sortKeys
# parameter: Dictionary
# Return: a list of the keys in alphabetical order
# Use the sort method on a list
def sortKeys(dictionary):
    sortedDictionary = sortKeys(dictionary)
    dictionaryList = [[k,v] for k,v in dictionary.items()]

# function: main
# call the readFile function to create a dictionary and store in it a variable
# call the sortKeys function to get a list of sorted keys and store it in a variabel
# Loop through the sorted keys list to print each letter and its frequency (number of times it occurs) using the dictionary.
def main():
    readFile()
    sortKeys()
    print("Displaying letter frequency of the Gettysburg Address")
    for key, value in dictionaryList:
        print(key, value)

main()

最佳答案

当然:

from string import ascii_lowercase

def readFile(filename = "gettysburg.txt"):
    with open(filename) as f:
        data = f.read().lower()
        letter_counts = {letter: data.count(letter) for letter in ascii_lowercase}

    return letter_counts

首先,更喜欢使用 with 而不是 open,因为如果您使用 open,您需要记住关闭文件对象。

其次,您想要的基本上是字典理解:一种使用某种方式相关的键和值自动填充 dict 的方法。

这段代码的作用是遍历 ascii_lowercase,它是一个包含字母表中小写字母的字符串。每个字母都成为生成的 dict 中的一个键,对应的值是该字母在给定文本中的计数。

关于python - 我可以简化我的代码,这样我就不会单独写出字母表中的每个字母吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55272389/

相关文章:

dictionary - 常规嵌套映射更新值而不检查值的类型

python - 如何使用 iterrows 或 itertuples 有效地迭代以从旧数据帧创建新数据帧

python - 如何将 optgroups 添加到 django ModelMultipleChoiceField?

php - Laravel map () : How to alter objects and arrays?

python - 将等于 json 字典的字典转换

c# - 将 Dictionary<string, string> 转换为 xml 的简单方法,反之亦然

python - 从 psycopg2 获取字典

python - Nltk:从列表列表中消除停用词

python - 以元组为参数的新样式格式

python - 列表分配中的字典导致奇怪的输出