简体   繁体   中英

Using a Dictionary to Replace Values Over a List of Strings

I am trying to use a dictionary to quickly substitute in incorrect words in each string in a list.

NOTE: I am trying to create a custom function, and the only solution that I could create was a set of nested "for loops" , which was computationally prohibitive considering the actual data I need to manipulate was over 150,000 in length.

Consider this:

test_combine = ["cat", "dog", "Hello", "Johnny", "You",
 "123_happy_goose", "123_happy", "45_goose", "45_lamegoose"]

Now in reality, these are addresses, but I created this sample problem. I want to replace these with a dictionary:

replace_dic = {"cat":"dog", "_happy_":"_sad_", "_goose\\b":"duck", "Johnny":"john"}

My file currently only uses underscores, and I use them when matching to ensure only certain cases are replaced. Example: If I want to change only happy if it is a middle word, I would re.sub with "_happy_" so that only "123_happy_goose" is changed and not "123_happy."

Ideally, the correct function would return

["dog", "dog", "Addition", "Hello", "john", "You",
 "123_sad_goose", "123_happy", "45_duck", "45_lamegoose"]
def replace(lst,dct):
    ar = []
    for i in lst:
        for k in dct.keys():
            i=i.replace(k,dct[k])
        ar.append(i)
    return ar

test_combine = ["cat", "dog", "Hello", "Johnny", "You",
 "123_happy_goose", "123_happy", "45_goose", "45_lamegoose"]

replace_dic = {"cat":"dog", "_happy_":"_sad_", "_goose\\b":"duck", "Johnny":"john"}

print(replace(test_combine,replace_dic))

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