Create a copy of the list not referencing the contained objects

前端 未结 4 768
暗喜
暗喜 2020-12-22 12:40

Say I have a vector a defined as:

a = [[1,2,3],[-1,-2,-3]]

I have learned that to create a copy of the object a w

4条回答
  •  我在风中等你
    2020-12-22 13:10

    a[:] creates a shallow copy of the list.

    You can use the copy.deepcopy() function to recursively copy the objects, or use a list comprehension:

    b = [el[:] for el in a]
    

    This creates a new list object with shallow copies of the nested list objects in a.

提交回复
热议问题