How to deep copy a list?

前端 未结 8 1374
旧巷少年郎
旧巷少年郎 2020-11-22 03:07

I have some problem with a List copy:

So After I got E0 from \'get_edge\', I make a copy of E0 by calling \'E0_copy =

相关标签:
8条回答
  • 2020-11-22 03:34

    This is more pythonic

    my_list = [0, 1, 2, 3, 4, 5]  # some list
    my_list_copy = list(my_list)  # my_list_copy and my_list does not share reference now.
    

    NOTE: This is not safe with a list of referenced objects

    0 讨论(0)
  • 2020-11-22 03:37

    If the contents of the list are primitive data types, you can use a comprehension

    new_list = [i for i in old_list]
    

    You can nest it for multidimensional lists like:

    new_grid = [[i for i in row] for row in grid]
    
    0 讨论(0)
提交回复
热议问题