对于循环和多个条件

2023-09-19 10:55 科技创新
| 在C ++中,可以说:
for (int i = 0; i < 100 && !found; i++) {
  if (items[i] == \"the one I\'m looking for\")
    found = true;
}
因此您不需要使用\“ break \”语句。 在Python中,我想您需要编写:
found = False

for item in items:
    if item == \"the one I\'m looking for\"
        found = True
        break
我知道我可以编写一个包含相同代码的生成器,以便隐藏此中断事件。但是我想知道是否还有其他方法可以实现同一件事(具有相同的性能)而无需使用额外的变量或while循环。 我知道我们可以说:
found = \"the one I\'m looking for\" in items
我只是想了解是否有可能在for循环中使用多个条件。 谢谢。