Jac*_*kie 4 python string python-3.x
我正在尝试比较两个不同字符串的第一个字符(依此类推),以根据这些结果形成一个新字符串.这是我尝试过的,但它将每个列表的每个元素相互比较.
def compare(a,b):
s = ""
for x in a:
for y in b:
if x == y:
s+=str(x)
else:
s+=str(y)
Run Code Online (Sandbox Code Playgroud)
这似乎是一个简单的问题,但我被卡住了.
使用zip:
def compare(a, b):
for x, y in zip(a, b):
if x == y:
...
Run Code Online (Sandbox Code Playgroud)