所以我有三个列表:
['this', 'is', 'the', 'first', 'list']
[1, 2, 3, 4, 5]
[0.01, 0.2, 0.3, 0.04, 0.05]
有没有一种方法可以让我按索引顺序打印这些列表中的值?
例如
this, 1, 0.01 (all items at list[0])
is, 2, 0.2 (all items at list[1])
the, 3, 0.3 (all items at list[2])
first, 4, 0.04 (all items at list[3])
list, 5, 0.05 (all items at list[4])
每次运行脚本时,每个列表中的项目数都会有所不同,但最终它们总是以相同数量的值结束。因此,有一次,脚本可以创建三个包含 30 个项目的数组,另一次,它只能在每个数组中创建 15 个值,等等。
最佳答案
您可能要找的是 zip
:
>>> x = ['this', 'is', 'the', 'first', 'list']
>>> y = [1, 2, 3, 4, 5]
>>> z = [0.01, 0.2, 0.3, 0.04, 0.05]
>>> zip(x,y,z)
[('this', 1, 0.01), ('is', 2, 0.20000000000000001), ('the', 3, 0.29999999999999999), ('first', 4, 0.040000000000000001), ('list', 5, 0.050000000000000003)]
>>> for (a,b,c) in zip(x,y,z):
... print a, b, c
...
this 1 0.01
is 2 0.2
the 3 0.3
first 4 0.04
list 5 0.05
关于python - 在 Python 中,我可以按索引号顺序打印 3 个列表吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1615289/