2024-09-30 20:37:04 发布
网友
我有两个字符串,s1和s2,我需要找出这两个字符串中是否有共同的字母,并使其加倍,例如:
s1
s2
s1 = "Hello World" s2 = "lo" output = "Helllloo Woorlld"
我的代码:
我得到一个错误:
if d[s2[w]]: IndexError: string index out of range
将字符串视为字符列表并使用列表理解来构造输出字符串会更加容易:
output = ''.join([c*2 if c in s2 else c for c in s1])
将字符串视为字符列表并使用列表理解来构造输出字符串会更加容易:
相关问题 更多 >
编程相关推荐