当我运行以下代码时,出现错误:
def genSet(nums):
for i in range(0, len(nums)-1):
for x in range(0, len(nums)-1):
if nums[x] == nums[i]:
del nums[x]
return nums
a = [5, 4, 3, 5, 6, 7, 8, 5, 4, 3]
print genSet(a)
输出:
Traceback (most recent call last):
File "49.py", line 9, in <module>
print genSet(a)
File "49.py", line 4, in genSet
if nums[x] == nums[i]:
IndexError: list index out of range
据我所知(我用“print x, i”替换了 if 语句)两个 for 循环都很好,那么为什么索引超出范围?