没有类型没有属性索引

时间:2017-11-04 15:19:15

标签: python attributeerror

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'

如何修复此属性错误?

1 个答案:

答案 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)