And*_*024 3 python for-loop list
As with regards to python, when it comes to a for loop, will it re-evaluate the upper bound before re-iteration?
Say I have the following scenario:
def remove(self,list,element):
for x in range(0,len(list)):
if somecondition:
list.pop(x)
Run Code Online (Sandbox Code Playgroud)
Will the len(list) condition be re-evaluated before executing the next iteration of the for loop? (As done in some languages such as Objective-C I believe) As otherwise if a number of elements is popped, an out of bounds error would arise if say 1 element was removed, and the last iteration would try to access list[len(list)-1].
I've tried to investigate this myself however the results are muddled each time.
编辑:我相信我的问题不同于标记为重复的问题,因为我的问题是关于循环继续进行下一次迭代的条件,因此可以轻松地添加一个元素而不是删除一个元素。
为了澄清起见,我的问题询问for循环条件是否将重新检查在下一次迭代之前提出的条件。
| 归档时间: |
|
| 查看次数: |
61 次 |
| 最近记录: |