我正在尝试将一个列表中的列表项插入另一个列表中。我找到了两种有效的解决方案,但对我来说似乎没有必要复杂。
我正在寻找的基本上是这样的列表:[1, 2, 4, 5, 3]
someList = [1, 2, 3]
anotherList = [4, 5]
Run Code Online (Sandbox Code Playgroud)
第一个解决方案:
for item in anotherList:
someList.insert(2, item)
Run Code Online (Sandbox Code Playgroud)
第二种解决方案:
someList = someList[:2]+anotherList[:]+someList[2:]
Run Code Online (Sandbox Code Playgroud)
我的直觉是使用类似的东西,但它将插入列表,而不是列表项。
someList.insert(2,anotherList)
Run Code Online (Sandbox Code Playgroud)