简体   繁体   中英

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

# 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


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

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  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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