简体   繁体   中英

Python: Compare two lists and update value in list2 based on value in list1

I have kinda a tricky problem I want to solve. I have two lists:

word = ['run', 'windless', 'marvelous']
pron = ['rVn', 'wIndl@s', 'mArv@l@s']

I want to do some processing that if the value in word contains "less", then the corresponding value in pron should turn to "lIs" instead of "l@s".

Desired output:

pron = ['rVn', 'wIndlIs', 'mArv@l@s']    

Any tips? It's troublesome to me because they're in two separate lists (but same order).

words = ['run', 'windless', 'marvelous']
prons = ['rVn', 'wIndl@s', 'mArv@l@s']


for (i, word) in enumerate(words):
    if "less" in word:
        prons[i] = prons[i].replace("l@s", "lIs")

print(prons)

Do you mean something like that?

>>> for i,w in enumerate(word):
...   if 'less' in w:
...     pron[i] = pron[i].replace('l@s','lIs')
... 
>>> pron
['rVn', 'wIndlIs', 'mArv@l@s']

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