i in range(len(x)):
if x[i]%2 == 0:
del x[i]
出错信息:
Traceback (most recent call last):
File "C:\Python36...\bar.py", line 3, in
if x[i]%2 == 0:
IndexError: list index out of range
出错原因分析:
Python列表在增加或删除元素时..., 10, 10]
[1, 3, 5, 7, 9, 10, 10]
[1, 3, 5, 7, 9, 10]
Traceback (most recent call last):
File "C:\Python36...0, 1, 2, 3, 5, 7, 9]
[0, 1, 2, 3, 5, 7, 9]
[0, 1, 3, 5, 7, 9]
[0, 1, 3, 5, 7, 9]
[1, 3, 5, 7, 9]
(2)不可下标错误...map、zip、enumerate、filter、生成器对象等类似的有序、惰性对象只能按序访问,在前面的元素访问之前不支持按位置直接访问后面的元素,也不支持切片操作,并且已访问过的元素不可以再次访问。