在文件中获取一个单词,并添加该单词所在的行号,并将其添加到列表中,然后添加到字典中

时间:2019-04-06 22:51:44

标签: python

我正在尝试编写一个函数索引,该索引带有一个参数,一个文件名(具有完整的故事),并返回一个字典,其中的键是文件中的单词,而值是包含行号唯一和以升序显示)。

要获得唯一键,我就是这样的:

    with open(filename) as file:
    text = file.read(); 
    list1 = set(text.split())
    print(list1)
    for line_num, line in enumerate(file):
        if any([word in line for word in list1]):
            print (line_num, line)

无法获得结果。

编辑:添加了示例数据:

LIET

Now, by Saint Peter's Church and Peter too,
He shall not make me there a joyful bride.
I wonder at this haste; that I must wed
Ere he, that should be husband, comes to woo.
I pray you, tell my lord and father, madam,
I will not marry yet; and, when I do, I swear,
It sh

答案应为-

{'LIET': [1], 'Now,': [3], 'by': [3], 'Saint': [3], "Peter's": [3], 'Church': [3], 'and': [3, 7], 'Peter': [3], 'too,': [3], 'He': [4], 'shall': [4], 'not': [4, 8], 'make': [4], 'me': [4], 'there': [4], 'a': [4], 'joyful': [4], 'bride.': [4], 'I': [5, 7, 8], 'wonder': [5], 'at': [5], 'this': [5], 'haste;': [5], 'that': [5, 6], 'must': [5], 'wed': [5], 'Ere': [6], 'he,': [6], 'should': [6], 'be': [6], 'husband,': [6], 'comes': [6], 'to': [6], 'woo.': [6], 'pray': [7], 'you,': [7], 'tell': [7], 'my': [7], 'lord': [7], 'father,': [7], 'madam,': [7], 'will': [8], 'marry': [8], 'yet;': [8], 'and,': [8], 'when': [8], 'do,': [8], 'swear,': [8], 'It': [9], 'sh': [9]})

2 个答案:

答案 0 :(得分:1)

以下应该可以解决问题

def index(filename):
    word_lines = {}
    with open(filename) as file:
        for line_num, line in enumerate(file.readlines(), 1):
            for word in line.split():
                if word in word_lines.keys(): 
                    if line_num not in word_lines[word]:
                        word_lines[word].append(line_num)
                else:
                    word_lines[word] = [ line_num ]
    return word_lines

print(index('test.txt'))

该示例的输出将是:

{'Now,': [1], 'by': [1], 'Saint': [1], "Peter's": [1], 'Church': [1], 'and': [1, 5], 'Peter': [1], 'too,': [1], 'He': [2], 'shall': [2], 'not': [2, 6], 'make': [2], 'me': [2], 'there': [2], 'a': [2], 'joyful': [2], 'bride.': [2], 'I': [3, 5, 6], 'wonder': [3], 'at': [3], 'this': [3], 'haste;': [3], 'that': [3, 4], 'must': [3], 'wed': [3], 'Ere': [4], 'he,': [4], 'should': [4], 'be': [4], 'husband,': [4], 'comes': [4], 'to': [4], 'woo.': [4], 'pray': [5], 'you,': [5], 'tell': [5], 'my': [5], 'lord': [5], 'father,': [5], 'madam,': [5], 'will': [6], 'marry': [6], 'yet;': [6], 'and,': [6], 'when': [6], 'do,': [6], 'swear,': [6], 'It': [7], 'sh': [7]}

答案 1 :(得分:0)

我将假定文件内容已在字符串text中读取。我还删除了标点符号

text = "He shall not make me there a joyful bride.\n " \
       "I wonder at this haste; that I must wed Ere he, that should be husband, comes to woo. \n" \
       "I pray you, tell my lord and father, madam, I will not marry yet;"
punctuations = '!"#$%&\'()*+,-./:;<=>?@[\]^_`{|}~'
for punctuation in punctuations:
    text = text.replace(punctuation, '')
words = list(set(text.split()))
print(words)
result_dict = {}
for line_num, line in enumerate(text.split('\n')):
    for word in words:
        if word in line:
            if word in result_dict.keys():
                result_dict[word].append(line_num)
            else:
                result_dict[word] = [line_num]

print(result_dict)
{
    'a': [0, 1, 2],
    'shall': [0],
    'bride': [0],
    'He': [0],
    'make': [0],
    'me': [0, 1],
    'not': [0, 2],
    'he': [0, 1, 2],
    'there': [0],
    'joyful': [0],
    'husband': [1],
    'and': [1, 2],
    'must': [1],
    'at': [1, 2],
    'wonder': [1],
    'to': [1],
    'I': [1, 2],
    'wed': [1],
    'this': [1],
    'Ere': [1],
    'comes': [1],
    'woo': [1],
    'that': [1],
    'haste': [1],
    'be': [1],
    'should': [1],
    'father': [2],
    'will': [2],
    'pray': [2],
    'my': [2],
    'yet': [2],
    'you': [2],
    'tell': [2],
    'lord': [2],
    'marry': [2],
    'madam': [2]
}