【问题标题】:Insert the contents of one list into another list at a specific index将一个列表的内容插入到另一个列表的特定索引处
【发布时间】:2013-10-16 04:07:59
【问题描述】:

我有两个列表

list1 = [1,2,3,4,5]
list2 = [6,7]

我想将list2的内容插入到list12-th索引中得到结果

list1 = [1,2,6,7,3,4,5]

我试过了:

list1.insert(2,list2)[1,2,[6,7],3,4,5]

temp = list1[:2]
temp.extend(list2)
temp.extend(list1[2:])
print(temp)

不过好像不是个好办法

有什么办法可以这样做

list1.insert(2,extend(list2))

【问题讨论】:

    标签: list python-3.x


    【解决方案1】:
    >>> list1 = [1,2,3,4,5]
    >>> list2 = [6,7]
    >>> list1[2:2] = list2
    >>> list1
    [1, 2, 6, 7, 3, 4, 5]
    

    【讨论】:

      猜你喜欢
      • 2011-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 2012-02-10
      • 2017-06-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多