我想知道它是否安全(有记录的行为?)删除Python中执行的迭代器的域空间。你知道吗
考虑代码:
import os
import sys
sampleSpace = [ x*x for x in range( 7 ) ]
print sampleSpace
for dx in sampleSpace:
print str( dx )
if dx == 1:
del sampleSpace[ 1 ]
del sampleSpace[ 3 ]
elif dx == 25:
del sampleSpace[ -1 ]
print sampleSpace
“sampleSpace”就是我所说的“迭代器的域空间”(如果有更合适的词/短语,请告诉我)。你知道吗
我所做的是在迭代器'dx'运行时从中删除值。你知道吗
以下是我对代码的期望:
Iteration versus element being pointed to (*):
0: [*0, 1, 4, 9, 16, 25, 36]
1: [0, *1, 4, 9, 16, 25, 36] ( delete 2nd and 5th element after this iteration )
2: [0, 4, *9, 25, 36]
3: [0, 4, 9, *25, 36] ( delete -1th element after this iteration )
4: [0, 4, 9, 25*] ( as the iterator points to nothing/end of list, the loop terminates )
。。我得到的是:
[0, 1, 4, 9, 16, 25, 36]
0
1
9
25
[0, 4, 9, 25]
如你所见-我所期望的是我得到的-这与我在这种情况下从其他语言得到的行为是相反的。你知道吗
因此,我想问一下,在Python中是否有类似“如果在迭代过程中改变迭代器的空间,那么迭代器将变得无效”这样的规则?你知道吗
是否安全(记录行为?)用Python来做这样的事情?你知道吗
你说的安全是什么意思?您的代码碰巧不会引发任何错误,但这是一种明显的可能性,当然,请考虑以下几点:
不清楚为什么要这样做,但是没有适用于迭代器的附加规则。他们的行为和其他类型的人完全一样。你知道吗
从the Python tutorial:
一般来说不,这不安全,你可能会有不可预知的行为。在这种情况下,迭代器不需要以特定的方式运行。你知道吗
在你的例子中发生的是
相关问题 更多 >
编程相关推荐