nums= [1,2,2,3]
def removnt(nums):
for n in nums:
i = nums.index(n)
if nums[i]==nums[i+1]:
nums = nums.remove(n)
removnt(nums)
我得到的错误是
Traceback (most recent call last):
File "source_file.py", line 20, in <module>
removnt(nums)
File "source_file.py", line 9, in removnt
i = nums.index(n)
AttributeError: 'NoneType' object has no attribute 'index'
如何修复此属性错误?
答案 0 :(得分:0)
这是一个应该做你想要的版本:
nums= [1,2,2,3]
def removnt(nums):
i = 0
while i < len(nums) - 1:
if nums[i] == nums[i+1]:
del nums[i]
else:
i += 1
removnt(nums)