我正在尝试将一个列表中的列表项插入另一个列表中。我找到了两个可行的解决方案,但对我来说似乎不必要。
我正在寻找的基本上是这样的列表: [1, 2, 4, 5, 3]
someList = [1, 2, 3]
anotherList = [4, 5]
第一个解决方案:
for item in anotherList:
someList.insert(2, item)
第二种解决方案:
someList = someList[:2]+anotherList[:]+someList[2:]
我的直觉是使用这样的东西,但是它将插入列表,而不是列表项。
someList.insert(2,anotherList)
有一个解决方案:
someList[2:2] = anotherList
这避免了插入None
条目的需要,并产生了所需的答案。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句