如何遍历2个数组?

时间:2019-02-16 13:00:25

标签: python list for-loop

我有2个不同的数组,我需要在打印两个数组时都需要帮助

Route = ["Bus A","Bus B","Bus C","Bus D","Bus E","Bus F"]
DaysLate = [ [1],[2],[3],[4],[5],[6] ]

有没有办法我可以得到这个输出?

Bus A 1
Bus B 2
Bus C 3
Bus D 4
Bus E 5
Bus F 6

3 个答案:

答案 0 :(得分:3)

尝试一下:

Route = ["Bus A","Bus B","Bus C","Bus D","Bus E","Bus F"]
DaysLate = [ [1],[2],[3],[4],[5],[6] ]

for i,j in zip(Route,DaysLate):
    print(i, j[0])

答案 1 :(得分:0)

尝试一下:

for i in range(6):
    print(Route[i], DaysLate[i][0])

答案 2 :(得分:0)

您还可以使用chain.from_iterable()模块中的函数itertools将所有子列表链接为一个序列:

for i, j in zip(Route, itertools.chain.from_iterable(DaysLate)):
    print(i, j)

或者,您可以使用星号*来打开子列表的包装:

for i, j in zip(Route, DaysLate):
    print(i, *j)