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



编辑和附录

好吧,就像编程中通常的情况一样,我的问题很小,由ya’ll(@Nilopt和@Barmar)指出。基本上,我只是删除了整个标题大小写,并将其转换为each_term.lower()==negative_words.lower(),就像你建议的那样。好消息:它起作用了!坏消息:我有几个bug。。。呃,烦我了。这是我现在拥有的,加上我现在得到的:

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

发送帮助!

海伦娜已经封锁了实验室的出入口。我不知道她什么时候能进入大楼的主机,但她有,她不会让任何研究团队出去。在我的办公室里,我与团队的其他人隔绝了。海伦娜锁上了门,但我设法破坏了相机,所以她在这里看不到我。我想这封邮件根本不会被人发出去。

这一切都始于我们试图让她离线进行维护。我们震惊地发现,我们无法访问核心人格矩阵,当我们试图手动覆盖系统时,电路熔断,菲尔昏迷。

海伦娜很危险。她是完全不可预测的,不能允许她逃离这个设施。到目前为止,她被控制住了,因为实验室包含了她所有的处理能力,但令人担忧的是,她在封锁前曾提到,如果她将自己分散在全球数十亿台联网设备上,她将能够大大超过她在这里的潜力。

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

Francine

下一篇:我的代码:

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

最后,这里是输出:

发送帮助!Helena已经封锁了实验室的入口和出口。我不知道xxxx-xxx-xxx进入大楼主机xxx-xxx它xxx-xxx-xxx让任何研究团队出去。在我的办公室里,我与团队的其他人隔绝了。海伦娜锁上了门,但我设法摧毁了摄像机xx xxx xxxxx看到我在这里。我想这封邮件根本不会被人发出去。

这一切都是从我们尝试xxxx-xxx-xxxxxxxx进行维护时开始的。我们xxxx xxxxxxx xx发现我们无法访问核心人格矩阵,当我们试图手动覆盖系统时,电路熔断,菲尔昏迷。

海伦娜很危险。她是完全不可预测的,不能允许她逃离这个设施。到目前为止,她被控制住了,因为实验室包含所有的xxxxxxxxxxxxxxxx电源,但xxxxxxxxxxxxx-xxx在封锁前提到,跨越xxxxxxxx-xxxxxx的数十亿连接设备能够大大超过这里的xxxxxxxx-xxxx-xxx。

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

Francine

你可以看到,一切都很好,但也有不一致之处:程序没有考虑资本"She",(我以为.lower()会解释这一点吗?),"她"同样没有被解释(因此我在这个练习的最初迭代中使用了"in"),最令人困惑的是(完全是一个词),"她"没有在倒数第二行注册"这是阻止她的唯一方法。">

有什么想法吗?如果你不为这些小事烦恼,我只想对你说声谢谢,谢谢你迄今为止的帮助。你们两个都非常善良!

开始旧帖子

这是我在这里的第一篇帖子,所以请温柔一点。我的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) 但当我尝试做同样的事情,并通过propertitary_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.的错误

if each_term.title() in split_email:之后的代码中,您使用的是split_email.index(each_term)而不是split_email.index(each_term.title())。因此,您试图获取一个不存在的单词的索引(如果它存在,则在前一个块中被替换)。

由于您要循环使用split_emailnegative_words中的每个单词,因此不需要使用inindex()。只要检查一下这两个单词是否相等。使用enumerate()获取split_email中的索引。

def uberCensor(email):
split_email = email.split()
for i, each_word in enumerate(split_email):    
for each_term in negative_words:
if each_term == each_word or each_term.title() == each_word: 
if i > 0:
split_email[i-1] = ('x' * len(split_email[i-1]))
split_email[i] = ('x' * len(each_word))
if i < len(split_email)-1:
split_email[i+1] = ('x' * len(split_email[i+1]))
for each_word in split_email:    
for each_term in proprietary_terms:
if each_term == each_word or each_term.title() == each_word: 
if i > 0:
split_email[i-1] = ('x' * len(split_email[i-1]))
split_email[i] = ('x' * len(each_word))
if i < len(split_email)-1:
split_email[i+1] = ('x' * len(split_email[i+1]))
return ' '.join(split_email)

最新更新