在python列表中将re.sub转换为多个值,但不具有寄存器意义

2024-10-02 12:38:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些字符串的列表my_list

['Dog Austin', 'Cat Piter', 'Tiger John', 'Lion Albert']

我有一本字典,上面有我的单词:我需要将它们替换为:

my_dict = {"Cat" : "Catty", "Dog" : "Doggy"}

我不需要寄存器大小写,这意味着算法应该是搜索偶数cAtDOg。我知道我需要使用re.IGNORECASE但是在哪里

我怎样才能做到这一点呢

这是一个很好的方法,但这里的输入只是一个字符串

s='hello, this is me'
replacements=[("hello", "hi"), ("this", "it's")]
for pat,repl in replacements:
    s = re.sub(pat, repl, s)

我有一个字符串列表的输入

我的输出需要

['Doggy Austin', 'Catty Piter', 'Tiger John', 'Lion Albert']


Tags: 字符串rehello列表myjohncattiger

热门问题