所以我必须编写一个程序:
在屏幕上打印,按照专辑数量的降序排列,每个乐队的一行。每行应包含乐队的名称,后跟冒号和空格,然后是该乐队的专辑数量。这看起来像这样:
所以下面有我的代码,但是我不断收到大量的错误,这些错误告诉我,当它们被定义时,事情并没有被定义,而且我也会得到这个 - > TypeError:' NoneType'对象不可迭代,任何帮助都会很棒!
import fileinput
import os
filename = open("albums.txt", "r") # open album.txt file
def process_line(line):
line = line.lower()
new_line = ""
for letter in line:
if letter in (""",.!"'()"""):
continue
elif letter == '-':
letter = ' '
new_line = new_line + letter
words = new_line.split()
return words
def count_words(filename):
if (os.path.isfile(filename) == False):
print("\nError: file " + filename + " does not exist.\n")
return
#in_file = open(filename, "r")
result = {}
for line in filename:
words = process_line(line)
for word in words:
if (word in result):
result[word] += 1
else:
result[word] = 1
def print_word_frequencies(dictionary):
print()
inverse = inverse_dictionary(dictionary)
frequencies = inverse.keys()
frequencies = list(frequencies) # convert frequencies to a list, so that we can sort it.
frequencies.sort() # sorting the list
frequencies.reverse() # reverse the sorting of the list
for frequency in frequencies: # for words with the same frequency, we want them sorted in
list_of_words = inverse[frequency]
list_of_words.sort() # sorting in alphabetical order
for word in list_of_words:
print(word + ":", frequency)
def inverse_dictionary(in_dictionary):
out_dictionary = {}
for key in in_dictionary:
value = in_dictionary[key]
if (value in out_dictionary):
list_of_keys = out_dictionary[value]
list_of_keys.append(key)
else:
out_dictionary[value] = [key]
return out_dictionary
def main():
filename = "albums.txt"
dictionary = count_words(filename)
print_word_frequencies(dictionary)
main()
答案 0 :(得分:1)
由于这是一项任务,我不会给你完整的代码,只是指出一些错误。
count_words
方法未返回任何内容,因此dictionary
为None
,TypeError: 'NoneType' object is not iterable
inverse_dictionary
for line in filename
时,您正在迭代文件名中的字符,而不是文件中的行,因为全局变量filename
被{隐藏了{1}}参数。使用filename
with open(filename) as the_file:
方法似乎很奇怪。看起来你删除了所有特殊字符,但是你打算如何分离乐队名称和专辑名称?你似乎只计算单词,而不是每个乐队的专辑。试试process_line
来获得乐队。line.split(" - ")[0]
中,只需使用某个自定义print_word_frequencies
函数对items
中的dictionary
进行排序,然后按计数排序。通过这些提示,您应能够修复您的程序。 (如果您想知道,我将您的程序缩减到大约十行代码。)