除了设flag,有没有什么方法能跳过下一次循环?

2013-02-05 17:51:57 +08:00
 Sin
像这样:
for i in [1,2,3]:
 if i==1:
  if xxx:
   跳过下一次循环,直接进入i=3的情况
3767 次点击
所在节点    Python
10 条回复
wog
2013-02-06 01:42:36 +08:00
for i in [1,2,3]:
 if i==2:
conitnue
print(i)
wog
2013-02-06 01:43:25 +08:00
囧continue都拼错了,你改一下吧
BOYPT
2013-02-06 09:52:57 +08:00
不可能。
VYSE
2013-02-06 10:02:26 +08:00
模仿C循环方式不就行了
yujnln
2013-02-06 10:06:58 +08:00
print [i for i in [1,2,3] if i!=2]
sivacohan
2013-02-06 21:06:37 +08:00
goto~
dotbuddle
2013-02-06 22:39:49 +08:00
列表是不是也有index?
Narcissu5
2013-02-07 00:22:05 +08:00
用枚举器循环,continue的同时next一下(好像更麻烦的说-_-!)
yuelang85
2013-02-07 00:55:55 +08:00
如果是遍历列表的话,可以remove/pop掉下一个元素

>>> l = [1,2,3,4]
>>> for i, v in enumerate(l):
... if v == 2:
... x = l.pop(i+1)
... print v
...
1
2
4
yuelang85
2013-02-07 00:56:24 +08:00
>>> l = [1,2,3,4]
>>> for i, v in enumerate(l):
... ...if v == 2:
... ......x = l.pop(i+1)
... ...print v
...
1
2
4

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

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

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

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

© 2021 V2EX