【发布时间】:2012-01-15 06:46:43
【问题描述】:
【问题讨论】:
【问题讨论】:
这是一个简单的例子:
for letter in 'Django':
if letter == 'D':
continue
print("Current Letter: " + letter)
输出将是:
Current Letter: j
Current Letter: a
Current Letter: n
Current Letter: g
Current Letter: o
它继续循环的下一次迭代。
【讨论】:
continue 的作用,但它并不过分有用,当你可以做if letter != 'D': print 'Current Letter:', letter
我喜欢在循环中使用 continue ,在这些循环中,在“开始工作”之前需要满足很多条件。所以不是这样的代码:
for x, y in zip(a, b):
if x > y:
z = calculate_z(x, y)
if y - z < x:
y = min(y, z)
if x ** 2 - y ** 2 > 0:
lots()
of()
code()
here()
我得到这样的代码:
for x, y in zip(a, b):
if x <= y:
continue
z = calculate_z(x, y)
if y - z >= x:
continue
y = min(y, z)
if x ** 2 - y ** 2 <= 0:
continue
lots()
of()
code()
here()
通过这种方式,我避免了非常深的嵌套代码。此外,通过首先消除最频繁发生的情况来优化循环很容易,因此我只需要处理不常见但重要的情况(例如除数为 0),当没有其他显示停止时。
【讨论】:
continue 类似于使用GOTO。然而,这是使用GOTO的正确方式。
通常需要/有用的情况是,当您想要跳过循环中的剩余代码并继续迭代时。
我真的不认为这是必要的,因为您始终可以使用 if 语句来提供相同的逻辑,但它可能有助于提高代码的可读性。
【讨论】:
if <condition>: continue 而不是if not <condition>: ... 可以避免在没有它的情况下编写时需要的缩进级别。
continue 语句时,我们实际上是在跳出条件测试部分并允许循环的迭代继续下一次迭代?在我看来,这比使用else 更好。仅仅是为了提高可读性和运行时性能吗?
import random
for i in range(20):
x = random.randint(-5,5)
if x == 0: continue
print 1/x
continue 是一个极其重要的控制语句。上面的代码是一个典型的应用,可以避免被零除的结果。当我需要存储程序的输出时,我经常使用它,但如果程序崩溃了,我不想存储输出。请注意,要测试上面的示例,请将最后一条语句替换为 print 1/float(x),否则只要有小数,您就会得到零,因为 randint 返回一个整数。为了清楚起见,我省略了它。
【讨论】:
有些人评论了可读性,说“哦,这对可读性没有多大帮助,谁在乎呢?”
假设您需要在主代码之前进行检查:
if precondition_fails(message): continue
''' main code here '''
请注意,您可以在编写主代码之后执行此操作,而无需更改该代码。如果您对代码进行比较,则只会突出显示带有“继续”的添加行,因为主代码没有间距更改。
想象一下,如果您必须对生产代码进行中断修复,结果只是在 continue 中添加一行。当您查看代码时,很容易看出这是唯一的变化。如果您开始将主代码包装在 if/else 中,则 diff 将突出显示新缩进的代码,除非您忽略间距更改,这在 Python 中尤其危险。我认为,除非您遇到不得不在短时间内推出代码的情况,否则您可能不会完全理解这一点。
【讨论】:
def filter_out_colors(elements):
colors = ['red', 'green']
result = []
for element in elements:
if element in colors:
continue # skip the element
# You can do whatever here
result.append(element)
return result
>>> filter_out_colors(['lemon', 'orange', 'red', 'pear'])
['lemon', 'orange', 'pear']
【讨论】:
continue 语句添加 是什么?使用element not in colors 可以消除它,代码也一样可读。
假设我们要打印所有不是 3 和 5 的倍数的数字
for x in range(0, 101):
if x % 3 ==0 or x % 5 == 0:
continue
#no more code is executed, we go to the next number
print x
【讨论】:
if x %3 == 0 or x % 5 == 0:, pass, else:, print x
continue。我的结论是从来没有需要,但在某些情况下(比如这个),使用continue 的代码更具可读性。这是一个很好的例子。
这不是绝对必要的,因为它可以使用 IF 来完成,但它更具可读性并且运行时成本更低。
如果数据不满足某些要求,我会使用它来跳过循环中的迭代:
# List of times at which git commits were done.
# Formatted in hour, minutes in tuples.
# Note the last one has some fantasy.
commit_times = [(8,20), (9,30), (11, 45), (15, 50), (17, 45), (27, 132)]
for time in commit_times:
hour = time[0]
minutes = time[1]
# If the hour is not between 0 and 24
# and the minutes not between 0 and 59 then we know something is wrong.
# Then we don't want to use this value,
# we skip directly to the next iteration in the loop.
if not (0 <= hour <= 24 and 0 <= minutes <= 59):
continue
# From here you know the time format in the tuples is reliable.
# Apply some logic based on time.
print("Someone commited at {h}:{m}".format(h=hour, m=minutes))
输出:
Someone commited at 8:20
Someone commited at 9:30
Someone commited at 11:45
Someone commited at 15:50
Someone commited at 17:45
如您所见,continue 语句之后没有出现错误值。
【讨论】:
if 只能做continue 所做的事情,前提是所有代码都包含在一个块中。 continue 会跳过 if 块之外的代码。
例如,如果您想根据变量的值做不同的事情:
my_var = 1
for items in range(0,100):
if my_var < 10:
continue
elif my_var == 10:
print("hit")
elif my_var > 10:
print("passed")
my_var = my_var + 1
在上面的示例中,如果我使用break,解释器将跳过循环。但是使用continue它只会跳过 if-elif 语句并直接进入循环的下一项。
【讨论】:
my_var 的初始值为0。
continue不是一个好广告。
elif 应该是if。代码只是没有给人以您知道自己在做什么的外观。
【讨论】:
continue 只是跳过循环中的其余代码,直到下一次迭代
【讨论】: