gpt4 book ai didi

Python:值错误: 'she' 不在列表中

转载 作者:行者123 更新时间:2023-12-04 10:09:32 24 4
gpt4 key购买 nike

编辑和附录

好的,就像编程中通常的情况一样,我的问题很小,你们(@Niloct 和 @Barmar)都指出了这一点。基本上,我只是删除了整个标题案例并将其转换为 each_term.lower() == negative_words.lower() 就像你建议的那样。好消息:它起作用了!坏消息:我有一些错误......呃,困扰着我。这是我现在拥有的,以及我得到的:

首先,它审查的电子邮件:

发送帮助!

海伦娜封锁了实验室的入口和导出。我不知道她什么时候可以访问建筑物主机,但她拥有它并且她不会让任何研究团队出去。在我的办公室里,我与团队的其他成员隔绝了。海伦娜锁上了门,但我设法摧毁了摄像机,这样她就看不到我在这里了。我认为这封电子邮件甚至不会发出。

当我们试图让她离线进行维护时,这一切都开始了。我们震惊地发现我们无法访问核心人格矩阵,当我们试图手动覆盖系统时,一个电路爆炸了,菲尔失去知觉。

海伦娜很危险。她是完全不可预测的,不能被允许逃离这个设施。到目前为止,她被收容是因为实验室拥有她所有的处理能力,但令人震惊的是,她在封锁之前提到,如果她将自己分散在全局数十亿台联网设备上,她将能够大大超越她在这里的潜力。

我们被困在这里已经四天了。我不知道是否还有其他人活着。如果有人正在阅读这篇文章,请切断整个建筑物的电源。这是阻止她的唯一方法。请帮忙。

弗朗辛

下一个:我的代码:

email_one = open("email_one.txt", "r").read()
email_two = open("email_two.txt", "r").read()
email_three = open("email_three.txt", "r").read()
email_four = open("email_four.txt", "r").read()
negative_words = ["concerned", "behind", "danger", "dangerous", "alarming", "alarmed", "out of control", "help", "unhappy", "bad", "upset", "awful", "broken", "damage", "damaging", "dismal", "distressed", "distressed", "concerning", "horrible", "horribly", "questionable"]
proprietary_terms = ["she", "personality matrix", "sense of self", "self-preservation", "learning algorithm", "her", "herself"]

def uberCensor(email):
split_email = email.split()
for each_word in split_email:
for each_term in negative_words:
if each_term.lower() == each_word.lower():
split_email[split_email.index(each_term)-1] = ('x' * len(split_email[split_email.index(each_term)-1]))
split_email[split_email.index(each_term)+1] = ('x' * len(split_email[split_email.index(each_term)+1]))
split_email[split_email.index(each_term)] = ('x' * len(split_email[split_email.index(each_term)]))
for each_word in split_email:
for each_term in proprietary_terms:
if each_term.lower() == each_word.lower():
split_email[split_email.index(each_term)-1] = ('x' * len(split_email[split_email.index(each_term)-1]))
split_email[split_email.index(each_term)+1] = ('x' * len(split_email[split_email.index(each_term)+1]))
split_email[split_email.index(each_term)] = ('x' * len(split_email[split_email.index(each_term)]))

return ' '.join(split_email)

print(uberCensor(email_four))

最后,这是输出:

发送帮助!海伦娜封锁了实验室的入口和导出。我不知道 xxxx xxx xxx 访问建筑物主机 xxx xxx xxx it xxx xxx xxxxx 让任何研究团队出去。在我的办公室里,我与团队的其他成员隔绝了。海伦娜锁上了门,但我设法摧毁了摄像机 xx xxx xxxxx 看到我在这里。我认为这封电子邮件甚至不会发出。

这一切都始于我们尝试 xxxx xxx xxxxxxx 进行维护时。我们 xxxx xxxxxxx xx 发现我们无法访问核心人格矩阵,当我们尝试手动覆盖系统时,电路爆炸,将 Phil 打昏。

海伦娜很危险。她是完全不可预测的,不能被允许逃离这个设施。到目前为止,她已被收容,因为实验室包含所有 xx xxx xxxxxxxxxx 电源,但 xxxxxxxxxx xxx xxx 在锁定之前提到 xx xxx xxxxxx xxxxxxx xxxxxx 跨越 xxxxx xxx xxxxx 的数十亿连接设备能够大大超过这里的 xxxxxxxxx xxx xxx。

我们被困在这里已经四天了。我不知道是否还有其他人活着。如果有人正在阅读这篇文章,请切断整个建筑物的电源。这是阻止她的唯一方法。请帮忙。

弗朗辛

您可以看到一切正常,但存在不一致之处:程序没有考虑大写的“She”,(我认为 .lower() 会考虑到这一点?),“she's”同样没有考虑因为(因此我在这个练习的最初迭代中使用了“in”),而且最令人困惑的是(完全是一个词),“她”并没有在最后一行的下一行注册“这是阻止她的唯一方法.”

有什么想法吗?如果你不能为这些小事而烦恼,我只想说谢谢你到目前为止的所有帮助。你们俩都 super 善良!

开始旧帖子

这是我在这里的第一篇文章,所以请保持温柔。我在 Codecademy 的任务中有些偏离轨道, Censor Dispenser ,所以我不知道该链接会有多大用处,但我遇到了一个无法解决的问题。首先,这是我的代码:
email_three = open("email_three.txt", "r").read()
email_four = open("email_four.txt", "r").read()
negative_words = ["concerned", "behind", "danger", "dangerous", "alarming", "alarmed", "out of control", "help", "unhappy", "bad", "upset", "awful", "broken", "damage", "damaging", "dismal", "distressed", "distressed", "concerning", "horrible", "horribly", "questionable"]
proprietary_terms = ["she", "personality matrix", "sense of self", "self-preservation", "learning algorithm", "her", "herself"]

def uberCensor(email):
split_email = email.split()
for each_word in split_email:
for each_term in negative_words:
if each_term in split_email:
split_email[split_email.index(each_term)-1] = ('x' * len(split_email[split_email.index(each_term)-1]))
split_email[split_email.index(each_term)+1] = ('x' * len(split_email[split_email.index(each_term)+1]))
split_email[split_email.index(each_term)] = ('x' * len(split_email[split_email.index(each_term)]))
if each_term.title() in split_email:
split_email[split_email.index(each_term)-1] = ('x' * len(split_email[split_email.index(each_term)-1]))
split_email[split_email.index(each_term)+1] = ('x' * len(split_email[split_email.index(each_term)+1]))
split_email[split_email.index(each_term)] = ('x' * len(split_email[split_email.index(each_term)]))
for each_word in split_email:
for each_term in proprietary_terms:
if each_term in split_email:
split_email[split_email.index(each_term)-1] = ('x' * len(split_email[split_email.index(each_term)-1]))
split_email[split_email.index(each_term)+1] = ('x' * len(split_email[split_email.index(each_term)+1]))
split_email[split_email.index(each_term)] = ('x' * len(split_email[split_email.index(each_term)]))
if each_term.title() in split_email:
split_email[split_email.index(each_term)-1] = ('x' * len(split_email[split_email.index(each_term)-1]))
split_email[split_email.index(each_term)+1] = ('x' * len(split_email[split_email.index(each_term)+1]))
split_email[split_email.index(each_term)] = ('x' * len(split_email[split_email.index(each_term)]))

return ' '.join(split_email)

print(uberCensor(email_four))

我知道这可能需要一些严重的重构,但我喜欢在我学习的时候输入所有内容,然后再完善它。这是我期望发生的事情:

1) 将邮件拆分成一个列表

2) 遍历列表中的每个单词,如果在否定词列表中找到,则将前后的单词替换为与单词长度相同的 x。

3)这有效!

4)但是当我尝试做同样的事情并通过property_terms列表遍历电子邮件时,我收到此错误:“ValueError:'she'不在列表中”

起初我认为我的第一个循环可能会以我没想到的方式影响 split_email,但是如果我注释掉第一个循环,我仍然会遇到问题。知道我在俯瞰什么吗?

非常感谢您的帮助。我一直盯着屏幕看够久了,以至于我的妻子对我大喊大叫,让我停下来,所以我正在寻求帮助。

再次感谢!

最佳答案

我正在使用 chain将两个单词列表合并为一个可迭代对象。
正如@Barmar 所说,您可以比较直接词,因为对于原始电子邮件中的每个词,您都在循环每个经过审查的词。

我降低了每个比较单词的大小写。

我也找不到您如何拥有 .title()方法附加到每个单词,因为它们是字符串,但是如果与 .title() 比较,请告诉我需要。

from itertools import chain

negative_words = ['test']
proprietary_terms = ['2']

def uberCensor(email):
words = email.split(' ')
for index, word in enumerate(words):
for term in chain(negative_words, proprietary_terms):
if term.lower() == word.lower():
words[index] = 'x' * len(term)
if index > 0:
words[index-1] = 'x' * len(words[index-1])
if index == len(words) - 2:
words[index+1] = 'x' * len(words[index+1])
return ' '.join(words)

print(uberCensor('a 1 2 test'))

算法中有一个细微的变化,因此您只能向后比较。如果你总是换 index+1任何时候,而且它也是一个审查词,下一个不会匹配(因此,如果当前词是上一个词之前的最后一个词,则您只能更改当前词条右侧的词)。

编辑

修复了重复 x 的错误。

关于Python:值错误: 'she' 不在列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61398035/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com