创建一个字典,将文本文件的每个单词链接到文件中出现的行列表

时间:2017-02-19 14:42:31

标签: python python-2.7 dictionary

我想要的是构建一个函数,该函数接收文本文件作为参数,并返回一个字典,其中文本中的每个单词与文本中出现单词的行列表相关联。这就是我想出的:

def dictionary(file):
    in_file=open(file, 'r')
    words=[]
    d={}
    lines=in_file.readlines()

    for line in lines:
        words=words+line.split(' ')

    for j in words:
        for i in range(len(lines)):
            if j in lines[i]:
                d[j]=i
    return d

然而,这并不是我想要的,因为它只显示出现该单词的一个行索引(而不是列表中)。 提前谢谢。

2 个答案:

答案 0 :(得分:0)

您可以存储列表,而不是仅为字典中的每个单词存储一个外观值。当找到另一个匹配时,这可以很容易地更新:

{{1}}

答案 1 :(得分:0)

这是一个应该按照注释执行所需操作的函数:

def dictionary(filename):
    # Pass the function a filename (string)

    # set up a dict to hold the results

    result = dict()

    # open the file and pass it to enumerate
    # this combination returns something like a list of
    # (index i.e. line number, line) pairs, which you can 
    # iterate over with the for-loop

    for idx, line in enumerate(open(filename)):

        # now take each line, strip any whitespace (most notably, 
        # the trailing newline character), then split the 
        # remaining line into a list of words contained in that line

        words = line.strip().split()

        # now iterate over the list of words

        for w in words:

            # if this is the first time you encounter this word, 
            # create a list to hold the line numbers within 
            # which this word is found

            if w not in result:
                result[w] = []

            # now add the current line number to the list of results for this word

            result[w].append(idx)

    # after all lines have been processed, return the result
    return result

指向相关功能的一些链接(它们无法在注释中正确显示):

open

enumerate

strip