Python:比较两个列表并根据list1中的值更新list2中的值 [英] Python: Compare two lists and update value in list2 based on value in list1

查看:58
本文介绍了Python:比较两个列表并根据list1中的值更新list2中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点想解决的棘手问题.我有两个列表:

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']

我想做一些处理,如果word中的值包含较少",那么pron中的对应值应变成"lIs"而不是"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".

所需的输出:

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)

这篇关于Python:比较两个列表并根据list1中的值更新list2中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆