同一个单词上的键错误

2024-05-17 08:21:22 发布

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

我在试着用圣经的风格造句。但每当我运行它时,它都会在同一个单词上出现一个键错误。这是令人困惑的,因为它只使用自己的键,而且每次出错时都是同一个单词,尽管随机选择. 在

这是txt文件,如果您想运行它:ftp://ftp.cs.princeton.edu/pub/cs226/textfiles/bible.txt

import random

files = []
content = ""
output = ""

words = {}

files = ["bible.txt"]
sentence_length = 200

for file in files:
    file = open(file)
    content = content + " " + file.read()

content = content.split(" ")

for i in range(100):  # I didn't want to go through every word in the bible, so I'm just going through 100 words
    words[content[i]] = []
    words[content[i]].append(content[i+1])

word = random.choice(list(words.keys()))

output = output + word

for i in range(int(sentence_length)):
    word = random.choice(words[word])
    output = output + word

print(output)

Tags: intxtforoutputftprandomfilescontent
2条回答

KeyError出现在这一行:

word = random.choice(words[word])

它总是发生在“中间”这个词上。在

怎么办?”第100个字在课文中。 第100位是第一次看到。 其结果是“中间”本身从来没有作为键放入words。 因此KeyError。在

为什么程序这么快就到达这个单词?部分原因是这里的一个bug:

for i in range(100):
    words[content[i]] = []
    words[content[i]].append(content[i+1])

这里的bug是words[content[i]] = []语句。 每次你看到一个字, 为它重新创建一个空列表。 “中间”之前的单词是“the”。 这是一个很常见的词, 课文中的许多其他单词都有“the”。 既然words["the"]是{}, 尽管存在随机性,但问题往往会发生很多次。在

您可以修复创建words的错误:

for i in range(100):
    if content[i] not in words:
        words[content[i]] = []
    words[content[i]].append(content[i+1])

然后当你随机选择单词时, 我建议添加一个if word in words条件, 处理输入中最后一个单词的大小写。在

“中间”是源文本中的第101个单词,它是第一次出现。执行此操作时:

words[content[i]].append(content[i+1])

你在做一个键:值对但不能保证该值将等同于现有的键。所以当你用这个值来搜索一个键时,它不存在,所以你得到了一个KeyError。在

如果你把你的范围从100改为101,你会发现你的程序几乎可以工作。这是因为第102个单词是“of”,它已经出现在源文本中了。在

你想怎么处理这个边缘案件就看你了。你可以这样做:

^{pr2}$

它基本上会循环到源文本的开头,当你到达结尾时。在

相关问题 更多 >