是否可以遍历多个列表并从同一循环中的不同列表返回参数?
即,而不是 -
For x in trees:
Print(x)
For y in bushes:
Print(y)
像 -
For x,y in trees,bushes:
Print(x +"\n"+ y)
答案 0 :(得分:8)
您只需使用zip
或itertools.izip
:
for x, y in zip(trees, bushes):
print x, y
答案 1 :(得分:2)
您可以使用zip()
:
a=['1','2','2']
b=['3','4','5']
for x,y in zip(a,b):
print(x,y)
输出:
1 3
2 4
2 5