Python None类型没有属性索引 在所有位置都假定有适当的缩进。

Python None类型没有属性索引 在所有位置都假定有适当的缩进。,python,attributeerror,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) 回溯(最近一次呼叫最后一次): 文件“source_File.py”,第20行,在 removnt(nums) removnt中第9行的文件“source_Fi

我得到的错误是

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)
回溯(最近一次呼叫最后一次):
文件“source_File.py”,第20行,在
removnt(nums)
removnt中第9行的文件“source_File.py”
i=单位索引(n)
AttributeError:“非类型”对象没有属性“索引”

如何修复此属性错误?

以下是一个版本,它可以满足您的要求:

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'
nums=[1,2,2,3]
def removnt(nums):
i=0
而i
nums.remove(n)
修改到位,它不会返回任何内容。从列表中删除项目,同时对其进行迭代,不会以您希望的方式工作。谢谢。你能解释一下错误的原因吗?或者抛出这个错误的原因是什么?
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)