Python:值错误:“她”不在列表中

2024-04-25 04:45:01 发布

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

编辑和补遗

好的,就像编程中通常出现的情况一样,我的问题很小,ya'll(@Niloct和@Barmar)都指出了这一点。基本上,我只是删除了整个标题大小写的内容,并将其转换为每个词。lower()==否定词。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))

最后,这里是输出:

快来帮忙!海伦娜已经封锁了实验室的入口和出口。我不知道XXXXXXXXXXX进入大楼主机xxx xxx xxx it xxx xxx XXXXXXXXXXXXXX让任何研究小组离开。在我的办公室里,我与团队的其他成员断绝了联系。海伦娜锁上了门,但我设法摧毁了摄像机xx xxx xxxxx请看我在这里。我认为这封邮件甚至不会被发送出去

这一切都是从我们尝试XXXXXXXXXXXXXX进行维护时开始的。我们XXXXXXXXXXXXXX发现,我们无法访问核心人格矩阵,当我们试图手动覆盖系统时,一条电路爆炸,使菲尔失去知觉

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

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

弗朗辛

你可以看到,一切都很好,但也有不一致的地方:程序没有考虑资本“She”(我想.lower()可以解释这一点?)“She's”同样没有考虑(因此我在这个练习的最初迭代中使用“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)但当我尝试做同样的事情,并通过专有条款列表反复浏览电子邮件时,我得到了以下错误:“ValueError:‘她’不在列表中”

起初我认为我的第一个循环可能会以一种我没有预料到的方式影响split_电子邮件,但如果我对第一个循环进行注释,我仍然会遇到这个问题。你知道我忽略了什么吗

非常感谢你的帮助。我盯着屏幕看的时间够长了,以至于我妻子对我大喊大叫,叫我停下来,所以我排起队来寻求帮助

再次感谢


Tags: intxtforreadindexlenemailopen
2条回答

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)

我使用chain将两个单词列表合并成一个iterable。 正如@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的错误

相关问题 更多 >