声明的定义是continue
:
该
continue
语句继续循环的下一次迭代。
我找不到任何好的代码示例。
continue
有人可以建议一些必要的简单案例吗?
这是一个简单的例子:
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 ,在你“开始做生意”之前,有很多条件需要完成。所以不是这样的代码:
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 是必要/有用的情况是,当您想跳过循环中的剩余代码并继续迭代时。
我真的不认为这是必要的,因为您始终可以使用 if 语句来提供相同的逻辑,但它可能对提高代码的可读性很有用。
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 '''
请注意,您可以在编写主代码后执行此操作,而无需更改该代码。如果您对代码进行比较,则只会突出显示带有“继续”的添加行,因为主代码没有间距更改。
想象一下,如果您必须对生产代码进行中断修复,结果只是添加了一行继续。当您查看代码时,很容易看出这是唯一的变化。如果您开始将主代码包装在 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']
假设我们要打印所有不是 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 来完成,但它更具可读性并且在运行时也更便宜。
如果数据不满足某些要求,我会使用它来跳过循环中的迭代:
# 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
声明之后出现。
例如,如果您想根据变量的值做不同的事情:
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 语句并直接进入循环的下一项。
continue
只是跳过循环中的其余代码,直到下一次迭代