You asked two questions:
Deep vs. shallow copymatrix[:]is a shallow copy -- it only copies the elements directly
stored in it, and doesn’t recursively duplicate the elements of arrays or
other references within itself. That means:
a = [[4]]b = a[:]a[0].append(5)print b[0] # Outputs [4, 5], as a[0] and b[0] point to the same array
The same would happen if you stored an object in
a.
deepcopy()is, naturally, a deep copy -- it makes copies of each of its
elements recursively, all the way down the tree:
Returninga = [[4]]c = copy.deepcopy(a)a[0].append(5)print c[0] # Outputs [4], as c[0] is a copy of the elements of a[0] into a new array
return final.append(li)is different from calling
appendand returning
finalbecause list.append does not return the list object itself, it returns
None
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)