Python copy a list of lists
本问题已经有最佳答案,请猛点这里访问。
我使用的是python 3.4.1。对于单列
附言:我做的是
从文档的
The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances):
- A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.
- A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.
当你呼叫你的正则
原因你不通告你这个例子使用的是第一
也许理解搜索列表为:
1 | new_list = [x[:] for x in old_list] |
如果你是...though快比一层矩阵的列表是不理解,不只是使用
一个编辑区,浅拷贝,想安静的对象引用包含在列表中。这样的做法……
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | >>> this = [1, 2] >>> that = [33, 44] >>> stuff = [this, that] >>> other = stuff[:] >>> other [[1, 2], [33, 44]] >>> other[0][0] = False >>> stuff [[False, 2], [33, 44]] #the same problem as before >>> this [False, 2] #original list also changed >>> other = [x[:] for x in stuff] >>> other [[False, 2], [33, 44]] >>> other[0][0] = True >>> other [[True, 2], [33, 44]] >>> stuff [[False, 2], [33, 44]] #copied matrix is different >>> this [False, 2] #original was unchanged by this assignment |
它是非常简单的,只是做的是:
1 | b = a |
例如:
1 2 3 4 5 6 7 | >>> a = [1, 2, 3] >>> b = a >>> b.append(4) >>> b [1, 2, 3, 4] >>> a [1, 2, 3, 4] |