我想同时遍历三个数字列表,并对每个变量组合进行计算。 所以我可以稍后将所有计算的数据存储在一个 3 维数据框中。
我试过:
for (x,y,z) in [(x, y, z) for x in a for y in b for z in c]:
但这似乎不起作用,它只对 x 和 y 的每个值执行计算,而不是 z。
关于如何同时循环 z 有什么想法吗?
最佳答案
如何使用 zip:
for x, y, z in zip(a, b, c):
# Do something
用具体例子更新
users = ['alex', 'john']
uids = [502, 501]
shells = ['bash', 'tcsh']
for user, uid, shell in zip(users, uids, shells):
print('User: {}, UID: {}, Shell: {}'.format(user, uid, shell))
输出:
User: alex, UID: 502, Shell: bash
User: john, UID: 501, Shell: tcsh
关于python - 同时对三个变量数组进行for循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42981715/