[英]Python: Printing two lists in a for loop
我有兩個清單。
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
我認為這是您想要的:
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=' ')
for o in one:
print(o)
for t in two:
print(t)
只是稍有變化。
one=["first","second"]
two=[1,2]
for o in one:
print(o)
for t in two:
print(t)
您的代碼應為:
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.