Python:在for循环中打印两个列表

时间:2016-02-24 20:14:04

标签: python

我有两个清单。

one=["first","second"]
two=[1,2]
for o in one:
    for t in two:
        print(o)
        print(t)

,输出为:

first
1
first
2
second
1
second
2

以下是正确的输出:

first
1
2
second
1
2

还有一位,我的教授要求打印输出 -

first
1
second
2

4 个答案:

答案 0 :(得分:3)

我认为这就是你想要的:

for o in one:
    print(o)
    for t in two:
        print(t)

因此,每次运行内循环时,只打印一次字符串数。

编辑:

你可以这样做:

both = zip(one, two)
for tup in both:
  for val in tup:
    print(val, end=' ')

答案 1 :(得分:2)

for o in one:
    print(o)
    for t in two:
        print(t)

答案 2 :(得分:2)

稍微改变一下。

    one=["first","second"]
    two=[1,2]
    for o in one:
        print(o)
        for t in two:
            print(t)

答案 3 :(得分:2)

您的代码应为:

one=["first","second"]
two=[1,2]
for o in one:
    print(o)
    for t in two:
        print(t)