栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python: deepcopy(list) vs new_list = old_list[:]

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python: deepcopy(list) vs new_list = old_list[:]

You asked two questions:

Deep vs. shallow copy

matrix[:]
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:

a = [[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

Returning

return final.append(li)
is different from calling
append
and returning
final
because list.append does not return the list object itself, it returns
None



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/661826.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号