从文本fi按行号索引单词

2024-09-29 00:17:12 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我的家庭作业问题是让函数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'))

Tags: 函数no内容列表字典haveit单词
3条回答

这里有一个使用集合的简单方法,我将给你一个如何用文件来做的练习。在

In [14]: text = """I have no pride
    ...: I have no shame
    ...: You gotta make it rain
    ...: Make it rain rain rain"""

In [15]:

In [15]: from collections import defaultdict

In [16]: d = defaultdict(set)

In [17]: for i, line in enumerate(text.split('\n')):
    ...:     for each_word in line.split(' '):
    ...:         d[each_word].add(i)
    ...:
    ...:

In [18]: d
Out[18]:
defaultdict(set,
            {'I': {0, 1},
             'Make': {3},
             'You': {2},
             'gotta': {2},
             'have': {0, 1},
             'it': {2, 3},
             'make': {2},
             'no': {0, 1},
             'pride': {0},
             'rain': {2, 3},
             'shame': {1}})

我第一次用Python写东西,但这很管用:

def lineIndex(fName):
    d = {}
    with open(fName, 'r') as f:       
        content = f.readlines()
        lnc = 0
        result = {}
        for line in content:
            line = line.rstrip()
            words = line.split(" ")
            for word in words:
                tmp = result.get(word)
                if tmp is None:
                    result[word] = []
                if lnc not in result[word]:
                    result[word].append(lnc)

            lnc = lnc + 1

        return result

print(lineIndex('index.txt'))

试试这个

def lineIndex(fName):
    dic = {}
    i=0
    with open(fName, 'r') as f:       
        while True:
            x=f.readline()
            if not x:
                break
            i+=1
            for j in x:
                if j in dic:
                    dic[j].add(i)
                else:
                    dic[j]=set()
                    dic[j].add(i)
    print (dic)

print (lineIndex("index.txt"))

相关问题 更多 >