<分区>
child = []
parent = [1, 3, 5, 7, 9]
index = 2
child.append(parent[:index])
当我运行这段代码时,我没有返回一个子列表
child = [1, 3]
我得到一个子列表:
child = [[1, 3]]
是否有一种单行方法可以将一个列表的几个对象复制到另一个列表而不使其成为嵌套列表?
<分区>
child = []
parent = [1, 3, 5, 7, 9]
index = 2
child.append(parent[:index])
当我运行这段代码时,我没有返回一个子列表
child = [1, 3]
我得到一个子列表:
child = [[1, 3]]
是否有一种单行方法可以将一个列表的几个对象复制到另一个列表而不使其成为嵌套列表?
最佳答案
试试这个:
child.extend(parent[:index])
关于python - 将子列表作为对象而不是列表复制到主列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37933837/