python中的concordance文件(Why I get 'dict' has no attribute 'sort' error in my conco

编程入门 行业动态 更新时间:2024-10-05 21:15:18
python中的concordance文件(Why I get 'dict' has no attribute 'sort' error in my concordance program in Python)

任务是编写一个程序,提示输入文件名,然后生成该文件的索引。 防爆。 一致性是一个按字母顺序排列的索引,它显示文档中每个单词出现的行。 例如,此段落的一致性可能显示为:

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 2

Here 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 :[

更多推荐

本文发布于:2023-08-04 20:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1422288.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   dict   python   concordance   attribute

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!