所以我的家庭作业问题是让函数lineIndex
索引文本文件中的单词,并返回文本文件中每个单词的行号列表。整个输出必须在字典中返回。在
例如,文本文件中的内容如下:
I have no pride
I have no shame
You gotta make it rain
Make it rain rain rain`
我的教授希望输出是这样的:
^{pr2}$例如:“雨”这个词在第二行和第三行。(第一行总是从零开始)
这是到目前为止我的代码,但我需要有关算法的帮助。在
def lineIndex(fName):
d = {}
with open(fName, 'r') as f:
#algorithm goes here
print(lineIndex('index.txt'))
这里有一个使用集合的简单方法,我将给你一个如何用文件来做的练习。在
我第一次用Python写东西,但这很管用:
试试这个
相关问题 更多 >
编程相关推荐