python列表id(L)和id(L [:]) [英] python list id(L) and id(L[:])

查看:178
本文介绍了python列表id(L)和id(L [:])的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇id(L)id(L[:])之间的区别和关系,其中L是列表.

I'm curious about the difference and relationship between id(L) and id(L[:]), where L is a list.

官方文档中关于id()的内容是这样的:

The official documentation says this about id():

CPython实现细节:这是对象在内存中的地址.

CPython implementation detail: This is the address of the object in memory.

但是我不明白为什么id(L)id(L[:])具有不同的值.

But I don't understand why id(L) and id(L[:]) have different values.

推荐答案

L[:]生成列表的副本.它的一个完整的新列表包含与L相同的项目.因此,它存储在内存中的不同位置并且具有不同的ID.

L[:] produces a copy of the list. Its a whole new list contain the same items as L. As a result, its stored in a different place in memory and has a different id.

这篇关于python列表id(L)和id(L [:])的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆