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

Python 在文件中获取一个单词,并添加该单词出现的行号,然后将该行号添加到列表中并添加到字典中,python,Python,我试图编写一个函数索引,它接受一个参数,一个包含完整故事的文件名,并返回一个字典,其中键是文件中的单词,值是包含行号的列表,行号是唯一的,这些单词以升序出现 为了获得唯一的密钥,我这样做: with open(filename) as file: text = file.read(); list1 = set(text.split()) print(list1) for line_num, line in enumerate(file):

我试图编写一个函数索引,它接受一个参数,一个包含完整故事的文件名,并返回一个字典,其中键是文件中的单词,值是包含行号的列表,行号是唯一的,这些单词以升序出现

为了获得唯一的密钥,我这样做:

    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]})

下面的内容应该可以做到这一点

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]}

我将假定文件内容已经以字符串文本形式读取。我还删除标点符号

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]
}

实际输出或错误是什么,期望输出是什么?此外,从您的文件中提供示例数据也很有用。如果没有得到任何输出,则会出现问题。他不会让我成为一个快乐的新娘。我对这种匆忙感到惊讶;我必须在他(应该是丈夫)来求爱之前结婚。我请求你,告诉我的主和父亲,夫人,我还不会结婚;请检查下面的答案,对不起,我不想导入字符串,如果你能找到不使用导入的解决方案的话..那会更好导入字符串是从你的单词中去掉标点符号,我想这是你想要的?如果没有,我可以找到另一种方法去除标点符号?这个答案不考虑标点符号。