0
我有不知何故未能以下list.index
函数代码段:Python列表索引怪异行为
for line in flines:
for match in bad_data_regex.findall(line):
assert line in flines
index = flines.index(line)
flines.pop(index)
# other code that does not modify flines ....
我的代码失败的:
ScriptEngine.execute:Error in script Traceback (innermost last):
File "<string>", line 168, in ?
ValueError: list.index(x): x not in list
如果值确实是不该列表中,我应该在list.index
函数之前获得AssertionError
。
我在Java版本1.6.0_18下使用Jython 2.2.1。
这怎么可能?
编辑:
原来这是生成错误的代码是不是上面我所贴。我的Jython解释器给了我错误的行号,并碰巧碰到了上面的索引函数。
上面的代码确实按照我的意图工作,但我意识到这并不是在迭代时修改列表的最佳实践。我会在将来尽可能避免这种情况。
感谢。事实证明,上面的代码确实按预期工作,但我不会修改将来迭代的同一个列表。 –