简体   繁体   English

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

[英]Python: Printing two lists in a for loop

I have two lists. 我有两个清单。

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

and the output is: 输出为:

first
1
first
2
second
1
second
2

Here is what the correct output should be: 这是正确的输出应该是:

first
1
2
second
1
2

There one more ,my professor has asked to print the output- 还有,我的教授要求打印输出-

first
1
second
2

I think this is what you want: 我认为这是您想要的:

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

So that you only print the string number once per time you run the inner loop. 这样,每次运行内循环时,您只打印一次字符串号。

EDIT: 编辑:

you can do this: 你可以这样做:

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

Just a slight change. 只是稍有变化。

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

Your code should be this: 您的代码应为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM