Python – 当字符串与大写和小写字母混合时,有没有办法替换字符串?
python 173
原文标题 :Python – Is there a way to replace a string when it’s mixed with uppercase and lowercase letters?
如果一个单词中有 d’ 或 ‘s,我想用 d’$ 和 $’s 替换它。
使用word.lower(),检查大写单词是否包含d’和’s就完成了。但是不知道如何用d’$和$’s替换大写字符串。有没有办法改它不使用正则表达式?
x = "D'you d'yeh IT'S she's love"
x = x.split()
b = ["d'", "'s"]
a = ["d'$", "$'s"]
y = x
for b, a in zip(b, a):
y = [word.replace(b, a) if b in word.lower() else word for word in y]
print(y)
目前的结果如下。
["D'you", "d'$yeh", "IT'S", "she$'s", 'love']
我想要的结果如下。
["D'$you", "d'$yeh", "IT$'S", "she$'s", 'love']
非常感谢您的帮助…