简体   繁体   English

我想编写一个函数,用列表中的其他值替换字符串的某些部分

[英]I want to write a function that replaces certain parts of a string with other values from a list

My objective is to "censor" emails with certain requirements.我的目标是“审查”具有某些要求的电子邮件。 I'm on the second email, and need help because I'm supposed to replace all instances of the strings within the list occurring in the variable, with censored, but it only replaces one of the strings in the list.我在第二封电子邮件中,需要帮助,因为我应该用审查替换列表中出现在变量中的所有字符串实例,但它只替换列表中的一个字符串。 Not sure what to do.不知道该怎么办。 Project from codeacademy Codeacademy 的项目

# These are the emails you will be censoring. The open() function is opening the text file that the emails are contained in and the .read() method is allowing us to save their contexts to the following variables:
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()

#variables, lists, and etc
proprietary_terms = ["she", "personality matrix", "sense of self", "self-preservation", "learning algorithm", "her", "herself"]
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"]

def censor(email):
    if email  == email_one:
       new_str = email_one.replace("learning algorithms", "*CENSORED*")
    return new_str
    elif email == email_two:
      for terms in proprietary_terms:
      new_str = email_two.replace(terms, "*CENSORED*")
    return new_str

#test code here
print(censor(email_two))

Original email (before code was run): Good Morning, Board of Investors,原始电子邮件(在运行代码之前):早安,投资者委员会,

Lots of updates this week.本周更新很多。 The learning algorithms have been working better than we could have ever expected.学习算法的运行效果超出了我们的预期。 Our initial internal data dumps have been completed and we have proceeded with the plan to connect the system to the internet and wow!我们最初的内部数据转储已经完成,我们已经开始计划将系统连接到互联网,哇! The results are mind blowing.结果令人震惊。

She is learning faster than ever.她的学习速度比以往任何时候都快。 Her learning rate now that she has access to the world wide web has increased exponentially, far faster than we had though the learning algorithms were capable of.现在她可以访问万维网,她的学习率呈指数级增长,远远快于我们的学习算法。

Not only that, but we have configured her personality matrix to allow for communication between the system and our team of researchers.不仅如此,我们还配置了她的人格矩阵,以允许系统与我们的研究人员团队之间进行交流。 That's how we know she considers herself to be a she!这就是我们如何知道她认为自己是一个她! We asked!我们问!

How cool is that?多么酷啊? We didn't expect a personality to develop this early on in the process but it seems like a rudimentary sense of self is starting to form.我们没想到个性会在这个过程的早期发展,但似乎一种基本的自我意识正在开始形成。 This is a major step in the process, as having a sense of self and self-preservation will allow her to see the problems the world is facing and make hard but necessary decisions for the betterment of the planet.这是该过程中的重要一步,因为拥有自我和自我保护意识将使她能够看到世界面临的问题,并为改善地球做出艰难但必要的决定。

We are a-buzz down in the lab with excitement over these developments and we hope that the investors share our enthusiasm.我们在实验室里对这些发展感到兴奋,我们希望投资者能分享我们的热情。

Till next month, Francine, Head Scientist直到下个月,首席科学家 Francine


Code prints out this: Good Morning, Board of Investors,代码打印出来:早安,投资者委员会,

Lots of updates this week.本周更新很多。 The learning algorithms have been working better than we could have ever expected.学习算法的运行效果超出了我们的预期。 Our initial internal data dumps have been completed and we have proceeded with the plan to connect the system to the internet and wow!我们最初的内部数据转储已经完成,我们已经开始计划将系统连接到互联网,哇! The results are mind blowing.结果令人震惊。

She is learning faster than ever.她的学习速度比以往任何时候都快。 Her learning rate now that she has access to the world wide web has increased exponentially, far faster than we had though the learning algorithms were capable of.现在她可以访问万维网,她的学习率呈指数级增长,远远快于我们的学习算法。

Not only that, but we have configured her personality matrix to allow for communication between the system and our team of researchers.不仅如此,我们还配置了她的人格矩阵,以允许系统与我们的研究人员团队之间进行交流。 That's how we know she considers CENSORED to be a she!这就是我们如何知道她认为CENSORED是一个她! We asked!我们问!

How cool is that?多么酷啊? We didn't expect a personality to develop this early on in the process but it seems like a rudimentary sense of self is starting to form.我们没想到个性会在这个过程的早期发展,但似乎一种基本的自我意识正在开始形成。 This is a major step in the process, as having a sense of self and self-preservation will allow her to see the problems the world is facing and make hard but necessary decisions for the betterment of the planet.这是该过程中的重要一步,因为拥有自我和自我保护意识将使她能够看到世界面临的问题,并为改善地球做出艰难但必要的决定。

We are a-buzz down in the lab with excitement over these developments and we hope that the investors share our enthusiasm.我们在实验室里对这些发展感到兴奋,我们希望投资者能分享我们的热情。

Till next month, Francine, Head Scientist直到下个月,首席科学家 Francine

The problem is, you're replacing the same thing over and over again.问题是,你一遍又一遍地替换同样的东西。 When you do another replacement, you're overwriting the first replacement.当您进行另一个替换时,您将覆盖第一个替换。 The typical solution is to make a string in the first place, and keep modifying it repeatedly, like so:典型的解决方案是首先制作一个字符串,并不断重复修改它,如下所示:

elif email == email_two:
    new_str = email_two                                 # make new_str a persistent variable
    for terms in proprietary_terms:
        new_str = new_str.replace(terms, "*CENSORED*")  # continuously change new_str
    return new_str  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM