任务是编写一个程序,提示输入文件名,然后生成该文件的索引。 防爆。 一致性是一个按字母顺序排列的索引,它显示文档中每个单词出现的行。 例如,此段落的一致性可能显示为:
Word Line Number a 1 1 2 alphabetical 1 an 1 appear 2在这里,我列出一个列表,以便我可以对单词进行排序。
我有这个代码:
f = open(raw_input("Enter a filename: "), "r") myDict = {} linenum = 0 for line in f: line = line.strip() line = line.lower() line = line.split() linenum += 1 for word in line: word = word.strip() word = word.lower() myDict[word] = linenum if word in myDict: myDict.sort() else: myDict.append(word) print "%-15s %-15s" %("Word", "Line Number") print "%-15s %-15d" %(myDict.keys(), myDict.values())当我现在运行该程序时,它说'dict'没有属性'sort'。 你能解释一下吗?
该文件与示例相同,输出也应该是上面的示例。 我是python的新手,请帮忙:[
The task is to write a program which prompts for a filename and then produces a concordance of that file. Ex. A concordance is an alphabetical index that shows the lines in a document where each word occurs. For example, a concordance for this paragraph might appear as:
Word Line Number a 1 1 2 alphabetical 1 an 1 appear 2Here I make a list so that I can sort the words.
I have this code:
f = open(raw_input("Enter a filename: "), "r") myDict = {} linenum = 0 for line in f: line = line.strip() line = line.lower() line = line.split() linenum += 1 for word in line: word = word.strip() word = word.lower() myDict[word] = linenum if word in myDict: myDict.sort() else: myDict.append(word) print "%-15s %-15s" %("Word", "Line Number") print "%-15s %-15d" %(myDict.keys(), myDict.values())When I run the program now it says 'dict' has no attribute 'sort'. Can you explain this please?
The file is the same as the example and the output should also be the example from above. I'm very new at python please help :[
更多推荐
发布评论