[英]how to make loop within loop go back to first loop
I have the a program to print items from lists within lists in a specific way. 我有一个程序可以以特定方式从列表中的列表中打印项目。 Here is that piece of code:
这是这段代码:
for y in range(0,5):
print '\n'
for x in tableau:
if y < len(x):
print x[y],
else :
print ' '
What I want is for the if statement go back to the inner loop(for x in tables) after it executes the print ' ' in the else part of if statement. 我想要的是if语句在if语句的else部分中执行print''之后返回内部循环(对于表中的x)。 Is there any way to do that?
有什么办法吗?
As I don't really understand what you need, I suggest you 2 solutions 由于我不太了解您的需求,因此建议您2种解决方案
First one: print ' ' instead of inexistent list element (3 times if len(x) == 2) 第一个:打印''而不是不存在的列表元素(如果len(x)== 2,则打印3次)
for x in tableau:
print '\n'
for y in x:
print y
for y in range(5 - len(x))
print ' '
Second one: print ' ' always at the end of the list 第二个:始终在列表末尾打印''
for x in tableau:
print '\n'
for y in x:
print y
print ' '
for y in range(0,5):
print '\n'
for x in tableau:
if y < len(x):
print x[y],
else :
print ' '
break
Will break out of the inner for and back into the outer for, after the print is executed. 执行打印后,将从内部for中断并返回外部。 This will print a "\\n" and then move back to the inner for, which I believe is what you are asking?
这将打印一个“ \\ n”,然后移回内部,我相信您在问什么?
As suggested by Delgan, the answer is staightforward - you have to use the break
keyword : 根据Delgan的建议,答案很简单-您必须使用
break
关键字:
for y in range(0,5):
print '\n'
for x in tableau:
if y < len(x):
print x[y],
else :
print ' '
break
The break keyword exits the most inner loop. break关键字退出最内部的循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.