Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有什么办法可以避免;在第一个空索引处插入“?”;?_Python_Python 3.x - Fatal编程技术网

Python 有什么办法可以避免;在第一个空索引处插入“?”;?

Python 有什么办法可以避免;在第一个空索引处插入“?”;?,python,python-3.x,Python,Python 3.x,我正在使用Python3,我想使用insert()函数在特定索引处插入Int。 我知道函数将首先尝试在指定索引之前的第一个空索引处插入它。有没有办法避免这种行为,先插入我指定的索引? 正在尝试执行以下操作: [18, 6, 6, 6, 1, -1] 但实际结果如下: [18, -1, 6, 1, 6, 6] 请发布用于获得实际结果的代码。“我知道函数将首先尝试在指定索引之前的第一个空索引处插入它。”嗯?你到底在干什么?。插入总是需要索引考虑到python是0索引的,您可以只在值+1处插入?正

我正在使用Python3,我想使用insert()函数在特定索引处插入Int。 我知道函数将首先尝试在指定索引之前的第一个空索引处插入它。有没有办法避免这种行为,先插入我指定的索引? 正在尝试执行以下操作:

[18, 6, 6, 6, 1, -1]
但实际结果如下:

[18, -1, 6, 1, 6, 6]

请发布用于获得实际结果的代码。“我知道函数将首先尝试在指定索引之前的第一个空索引处插入它。”嗯?你到底在干什么?。插入总是需要索引考虑到python是0索引的,您可以只在值+1处插入?正如目前提出的,这个问题没有任何意义。它指的是一个
insert
函数,但标准Python中唯一存在的函数是
list.insert
,它的行为与提问者暗示的完全不同(例如,因为在正常列表中没有“空”索引)。如果您指的是其他功能,您需要向我们展示它,否则我们无法回答任何有关它的问题!
def replaceElements(self, arr: List[int]) -> List[int]:
        res = []
        temp = -1
        for index in range(len(arr)-1, -1, -1):
            res.insert(index, temp)
            if arr[index] > temp:
                temp = arr[index]
        return res