在JavaScript中,我可以splice用来将多个元素的数组插入到数组中:myArray.splice(insertIndex, removeNElements, ...insertThese)。
splice
myArray.splice(insertIndex, removeNElements, ...insertThese)
但是,如果没有concat列表,我似乎无法找到一种在Python中执行类似操作的方法。有这种方法吗?(有关插入单个项目而不是多个项目的问题已得到解答。)
例如myList = [1, 2, 3],我想otherList = [4, 5, 6]通过调用myList.someMethod(1, otherList)get进行插入[1, 4, 5, 6, 2, 3]
myList = [1, 2, 3]
otherList = [4, 5, 6]
myList.someMethod(1, otherList)
[1, 4, 5, 6, 2, 3]
要扩展列表,只需使用list.extend。要插入索引中任何可迭代的元素,可以使用切片分配...
list.extend
>>> a = list(range(10)) >>> a [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> a[5:5] = range(3) >>> a [0, 1, 2, 3, 4, 0, 1, 2, 5, 6, 7, 8, 9]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
点击生成二维码
我来说两句