Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.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 2.7_List_Insert - Fatal编程技术网

Python 在列表中的特定索引处插入元素并返回更新后的列表

Python 在列表中的特定索引处插入元素并返回更新后的列表,python,python-2.7,list,insert,Python,Python 2.7,List,Insert,我有这个: >>> a = [1, 2, 4] >>> print a [1, 2, 4] >>> print a.insert(2, 3) None >>> print a [1, 2, 3, 4] >>> b = a.insert(3, 6) >>> print b None >>> print a [1, 2, 3, 6, 4] 有没有一种方法可以得到更新

我有这个:

>>> a = [1, 2, 4]
>>> print a
[1, 2, 4]

>>> print a.insert(2, 3)
None

>>> print a
[1, 2, 3, 4]

>>> b = a.insert(3, 6)
>>> print b
None

>>> print a
[1, 2, 3, 6, 4]

有没有一种方法可以得到更新后的列表,而不是将原始列表更新到位?

我得到的最短列表:
b=a[:2]+[3]+a[2:][/code>

>>>
>>> a = [1, 2, 4]
>>> print a
[1, 2, 4]
>>> b = a[:2] + [3] + a[2:]
>>> print a
[1, 2, 4]
>>> print b
[1, 2, 3, 4]
l.insert(index,obj)
实际上不返回任何内容。它只是更新列表

正如ATO所说,您可以执行
b=a[:index]+[obj]+a[index:][/code>。
然而,另一种方法是:

a = [1, 2, 4]
b = a[:]
b.insert(2, 3)
最高效的方法 还可以使用列表中的切片索引插入元素。例如:

>>> a = [1, 2, 4]
>>> insert_at = 2  # Index at which you want to insert item

>>> b = a[:]   # Created copy of list "a" as "b".
               # Skip this step if you are ok with modifying the original list

>>> b[insert_at:insert_at] = [3]  # Insert "3" within "b"
>>> b
[1, 2, 3, 4]
>>> a = [1, 2, 4]
>>> insert_at = 2   # Index starting from which multiple elements will be inserted

# List of elements that you want to insert together at "index_at" (above) position
>>> insert_elements = [3, 5, 6]

>>> a[insert_at:insert_at] = insert_elements
>>> a   # [3, 5, 6] are inserted together in `a` starting at index "2"
[1, 2, 3, 5, 6, 4]
对于在给定索引处同时插入多个元素的,只需使用要插入的多个元素的
列表。例如:

>>> a = [1, 2, 4]
>>> insert_at = 2  # Index at which you want to insert item

>>> b = a[:]   # Created copy of list "a" as "b".
               # Skip this step if you are ok with modifying the original list

>>> b[insert_at:insert_at] = [3]  # Insert "3" within "b"
>>> b
[1, 2, 3, 4]
>>> a = [1, 2, 4]
>>> insert_at = 2   # Index starting from which multiple elements will be inserted

# List of elements that you want to insert together at "index_at" (above) position
>>> insert_elements = [3, 5, 6]

>>> a[insert_at:insert_at] = insert_elements
>>> a   # [3, 5, 6] are inserted together in `a` starting at index "2"
[1, 2, 3, 5, 6, 4]
要了解有关切片索引的更多信息,请参考:

注意:在Python3.x中,切片索引和
list.index(…)
之间的性能差异显著减小,两者几乎相等。然而,在Python2.x中,这种差异非常明显。我在后面的回答中分享了性能比较


使用列表理解的备选方案(但在性能方面非常缓慢):

作为替代方案,也可以使用列表理解来实现。(但请不要这样做,这只是为了举例说明):


所有解决方案的性能比较 下面是对Python3.9.1和Python2.7.16上1000个元素列表中所有答案的比较。对于这两个Python版本,答案都是按性能顺序列出的

Python 3.9.1
  • 使用切片插入-最快(每个循环2.25微秒)

  • 使用
    列表进行多数投票。插入(…)
    -秒(每个循环2.33微秒)

  • 基于切片列表的合并-第三(每个循环5.01微秒)

  • 具有列表理解和枚举功能
  • -第四个(非常慢,每个循环135微秒)

    Python 2.7.16
  • 使用切片插入-最快(每个循环2.09微秒)

  • 使用
    列表进行多数投票。插入(…)
    -秒(每个循环2.36微秒)

  • 基于切片列表的合并-第三(每个循环4.44微秒)

  • 具有列表理解和枚举功能
  • -第四个(非常慢,每个循环103微秒)

    使用。用法:

    #语法

    insert()方法的语法−

    list.insert(索引,obj)

    #参数

    • 索引− 这是需要插入对象obj的索引
    • obj− 这是要插入到给定列表中的对象
    #返回值 此方法不返回任何值,但在给定索引处插入给定元素

    例如:

    a = [1,2,4,5]
    
    a.insert(2,3)
    
    print(a)
    

    返回
    [1,2,3,4,5]

    最干净的方法是复制列表,然后将对象插入副本中。在Python 3上,这可以通过
    列表来完成。复制

    new=old.copy()
    新增。插入(索引、值)
    
    在Python2上,可以通过
    new=old[:]
    来复制列表(这也适用于Python3)

    就性能而言,与其他拟定方法没有区别:

    $ python --version
    Python 3.8.1
    $ python -m timeit -s "a = list(range(1000))" "b = a.copy(); b.insert(500, 3)"
    100000 loops, best of 5: 2.84 µsec per loop
    $ python -m timeit -s "a = list(range(1000))" "b = a.copy(); b[500:500] = (3,)"
    100000 loops, best of 5: 2.76 µsec per loop
    

    b=a[:]。insert(2,3)
    看起来很短,不影响原始列表,而且很有描述性。@mkoistinen它对我不起作用<代码>>>>a=[1,2,3,4]>>>b=a[:]。插入(2,5)>>>print b None
    如果您不能容忍3行可读代码,请将其放入函数中并调用它。这并不能回答问题。问题是明确的:
    是否仍有我可以得到更新列表的结果,你的答案正好相反。我非常喜欢这个结果,因为它很容易扩展以解决问题,如果我想将值
    3,3.5
    按顺序插入该列表中,该怎么办?
    a[2:2]=[3,3.5]
    。很少的代码行数并不能很好地衡量代码质量。这种方法在性能和可读性方面都有缺陷。我们可以做
    a=a[:2]+[3]+a[2:][/code>?
    
    python3 -m timeit -s "a = list(range(1000))" "[y for i, x in enumerate(a) for y in ((3, x) if i == 500 else (x, )) ]"
    2000 loops, best of 5: 135 µsec per loop
    
    python -m timeit -s "a = list(range(1000))" "b = a[:]; b[500:500] = [3]"
    100000 loops, best of 3: 2.09 µsec per loop
    
    python -m timeit -s "a = list(range(1000))" "b = a[:]; b.insert(500, 3)"
    100000 loops, best of 3: 2.36 µsec per loop
    
    python -m timeit -s "a = list(range(1000))" "b = a[:500] + [3] + a[500:]"
    100000 loops, best of 3: 4.44 µsec per loop
    
    python -m timeit -s "a = list(range(1000))" "[y for i, x in enumerate(a) for y in ((3, x) if i == 500 else (x, )) ]"
    10000 loops, best of 3: 103 µsec per loop
    
    a = [1,2,4,5]
    
    a.insert(2,3)
    
    print(a)
    
    $ python --version
    Python 3.8.1
    $ python -m timeit -s "a = list(range(1000))" "b = a.copy(); b.insert(500, 3)"
    100000 loops, best of 5: 2.84 µsec per loop
    $ python -m timeit -s "a = list(range(1000))" "b = a.copy(); b[500:500] = (3,)"
    100000 loops, best of 5: 2.76 µsec per loop