continue 怎么跳到上一级的循环

2014-10-24 18:03:02 +08:00
 yakczh

举个例子
“A
arr=range(1,10)
skip=[3,5]

for i in arr:
for s in skip:
if i % s ==0:
continue
print(i)

本意是 被3或者5整除的数不打印, 但是这样写 貌似是跳过 skip的列表,怎么样满足条件后跳过arr
2196 次点击
所在节点    问与答
5 条回复
rrfeng
2014-10-24 20:26:40 +08:00
break
rrfeng
2014-10-24 20:28:39 +08:00
哦看错了……
skydiver
2014-10-24 20:36:19 +08:00
无所谓,print放里面就行了
yuelang85
2014-10-24 21:19:14 +08:00
打标记。

第一层循环一开始把flag设为0,第二层循环遇到条件就把flag设成1,紧接着break。这样就跳到了第一层循环,判断flag为1,continue。

for i in xrange(1, 10):
>>>>flag = 0
>>>>for x in xrange(1, 10):
>>>>>>>>if x == 2:
>>>>>>>>>>>>flag = 1
>>>>>>>>>>>>break
>>>>if flag == 1:
>>>>>>>>continue
picasso250
2014-10-24 21:52:56 +08:00
def can_print(i):
>>>>for s in skip:
>>>>>>>>if i % s == 0: return False
>>>>return True

for i in arr:
>>>>if can_print(i): print(i)

if you find a way hard, then u might choose the wrong way

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/141293

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX