如何同时为2个列表使用for循环,例如a = [AH,B,UB] b = [aa,b,yu]

时间:2016-05-02 07:33:59

标签: python

我希望AHaa同时获得两个不同的变量,可能称为caplowercase

for cap, lowercase in a,b:
    print(cap+lowercase)

zip不起作用,因为它只会从A读取AH

2 个答案:

答案 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