我理解这个结构是如何工作的:
for i in range(10):
print(i)
if i == 9:
print("Too big - I'm giving up!")
break;
else:
print("Completed successfully")
Run Code Online (Sandbox Code Playgroud)
但我不明白为什么else在这里用作关键字,因为它表明有问题的代码仅在for块未完成时运行,这与它的作用相反!无论我如何看待它,我的大脑都无法从for语句无缝地进展到else块.对我来说,continue或者continuewith更有意义(我正在努力训练自己阅读它).
我想知道Python编码器如何在他们的脑海中读取这个结构(或者如果你愿意的话,大声朗读).也许我错过了一些会让这些代码块更容易破译的东西?
Python有一个方便的语言功能叫做"for-else"(类似地,"while-else"),它看起来像这样:
for obj in my_list:
if obj == target:
break
else: # note: this else is attached to the for, not the if
print "nothing matched", target, "in the list"
Run Code Online (Sandbox Code Playgroud)
本质上,else如果循环中断,则跳过,但如果循环通过条件失败(for while)或迭代结束(for )退出,则运行for.
有没有办法做到这一点bash?我能想到的最接近的是使用一个标志变量:
flag=false
for i in x y z; do
if [ condition $i ]; then
flag=true
break
fi
done
if ! $flag; then
echo "nothing in the list fulfilled the condition"
fi
Run Code Online (Sandbox Code Playgroud)
这更加冗长.
在我正在处理的一些代码中,我有一个迭代遍历地图的for循环:
for (auto it = map.begin(); it != map.end(); ++it) {
//do stuff here
}
Run Code Online (Sandbox Code Playgroud)
我想知道是否有某种方法可以简明扼要地写出以下内容:
for (auto it = map.begin(); it != map.end(); ++it) {
//do stuff here
} else {
//Do something here since it was already equal to map.end()
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以改写为:
auto it = map.begin();
if (it != map.end(){
while ( it != map.end() ){
//do stuff here
++it;
}
} else {
//stuff
}
Run Code Online (Sandbox Code Playgroud)
但有没有更好的方法不涉及包装if语句?
是否有 Fortran 等价于 Python 的 for-else 语句?
例如,以下将数字列表排序为不同的范围。在 Python 中,它是:
absth = [1, 2, 3, 4, 5]
vals = [.1, .2, .5, 1.2, 3.5, 3.7, 16.8, 19.8, 135.60]
counts = [0] * len(absth)
for v in vals:
for i, a in enumerate(absth):
if v < a:
counts[i] += 1
break
else:
counts[-1] += 1
Run Code Online (Sandbox Code Playgroud)
在 Fortran 中,它的工作原理相同:
do iv = 1, nvals
is_in_last_absth = .true.
do ia = 1, nabsth - 1
if vals(iv) < absth(ia) then
counts(ia) = counts(ia) …Run Code Online (Sandbox Code Playgroud) 此代码运行良好,并生成所需的素数列表.但else打印我们的素数的块是不合适的,但无论如何它都可以工作,有人可以解释一下吗?
for num in range(0, 100 + 1):
# prime numbers are greater than 1
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)
Run Code Online (Sandbox Code Playgroud)
我不确定当该语句位于带有子句的循环continue内时如何解释该语句。forelse
如果条件为真,则将break从循环中退出for并且else部分将不会被执行。如果条件为 False,则else部分将被执行。
但是,声明呢continue?我测试了一下好像语句continue到达后,该else部分就会被执行。这是真的??这是一个代码示例:
# when condition found and it's `true` then `else` part is executing :
edibles = ["ham", "spam", "eggs","nuts"]
for food in edibles:
if food == "spam":
print("No more spam please!")
continue
print("Great, delicious " + food)
else:
print("I am so glad: No spam!")
print("Finally, I finished stuffing myself")`
Run Code Online (Sandbox Code Playgroud)
如果我从列表中删除“垃圾邮件”,现在条件总是false并且从未找到,但仍然else执行该部分:
edibles = ["ham","eggs","nuts"]
for food …Run Code Online (Sandbox Code Playgroud) 我试图检查列表中的所有元素,看看它们是否符合"小于5"的条件.我想要做的是如果我的列表中没有数字小于5,我想打印一条声明"此列表中没有小于5的元素",否则只打印那些数字,而不是"此列表中没有小于5的元素." 也.
list = [100, 2, 1, 3000]
for x in list:
if int(x) < 5:
print(x)
else:
print("There are no elements in this list less than 5.")
Run Code Online (Sandbox Code Playgroud)
这会产生输出:
2
1
There are no elements in this list less than 5.
Run Code Online (Sandbox Code Playgroud)
如何摆脱输出的最后一行?