我希望AH
和aa
同时获得两个不同的变量,可能称为cap
和lowercase
。
for cap, lowercase in a,b:
print(cap+lowercase)
zip
不起作用,因为它只会从A
读取AH
。
答案 0 :(得分:1)
zip不起作用
实际上,如果按照你的说法使用它(给它字符串列表),它确实如此:
li_a = ['AH', 'B', 'UB']
li_b = ['aa', 'b', 'yu']
for elem_a, elem_b in zip(li_a, li_b):
print('thing from a is {}, thing from b is {}'.format(elem_a, elem_b))
>> thing from a is AH, thing from b is aa
thing from a is B, thing from b is b
thing from a is UB, thing from b is yu
答案 1 :(得分:0)
如果您希望列表中的元素具有相同的索引 a 和 b 那么你可以根据它们的索引选择值 代码: -
a = ['T','UH','T']
b = ['t','oo','t']
for i in range(len(a)):
print(a[i]+b[i])
输出: -
Tt
UHoo
Tt
已编辑: -
** Pythonic做同样的方式**
a = ['T','UH','T']
b = ['t','oo','t']
print (map(lambda x,y:x+y, a,b))
编译并运行上面的soln。在 https://code.hackerearth.com/69a134p