文件输入频率排序

时间:2015-04-24 04:09:54

标签: python-3.x

所以我必须编写一个程序:

  • 将文件名作为参数。
  • 读取文件并为每个波段计算文件中列出的该乐队的专辑数量。 (http://vlm1.uta.edu/~cconly/teaching/cse1310_spring2015/assignments/assignment7/albums.txt
  • 在屏幕上打印,按照专辑数量的降序排列,每个乐队的一行。每行应包含乐队的名称,后跟冒号和空格,然后是该乐队的专辑数量。这看起来像这样:

    • band1:number1
    • band2:number2
    • band3:number3

所以下面有我的代码,但是我不断收到大量的错误,这些错误告诉我,当它们被定义时,事情并没有被定义,而且我也会得到这个 - > 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()

1 个答案:

答案 0 :(得分:1)

由于这是一项任务,我不会给你完整的代码,只是指出一些错误。

  • 首先,你的缩进是错误的,缩进在Python中很重要!当您将代码粘贴到问题编辑器中时可能会发生这种情况,但可能不会。特别要确保你没有混合标签和空格!
  • 您的count_words方法未返回任何内容,因此dictionaryNoneTypeError: '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进行排序,然后按计数排序。

通过这些提示,您能够修复您的程序。 (如果您想知道,我将您的程序缩减到大约十行代码。)