Iterate through multiple lists at the same time
本问题已经有最佳答案,请猛点这里访问。
是否可以遍历多个列表,并在同一循环中返回来自不同列表的参数?
即,代替-
1 2 3 4 | For x in trees: Print(x) For y in bushes: Print(y) |
有点像-
1 2 3 | For x,y in trees,bushes: Print(x +" "+ y) |
您只需使用
1 2 | for x, y in zip(trees, bushes): print x, y |
您可以使用
1 2 3 4 5 | a=['1','2','2'] b=['3','4','5'] for x,y in zip(a,b): print(x,y) |
输出:
1 3
2 4
2 5