con*_*293 0 python loops for-loop list
是否可以迭代多个列表并从同一循环中的不同列表返回参数?
即,而不是 -
For x in trees:
Print(x)
For y in bushes:
Print(y)
Run Code Online (Sandbox Code Playgroud)
就像是 -
For x,y in trees,bushes:
Print(x +"\n"+ y)
Run Code Online (Sandbox Code Playgroud)
你可以简单地使用 zip
或itertools.izip
:
for x, y in zip(trees, bushes):
print x, y
Run Code Online (Sandbox Code Playgroud)