在 python 中使用 id() 比较列表(整数)

发布于 2024-12-13 13:45:34 字数 367 浏览 0 评论 0原文

当我尝试比较由整数组成的列表时,我在 Python 中发现了一件奇怪的事情。

例如:

In [35]: id(range(1,5)),id(range(1,15)),id(range(16,0,-1))
Out[35]: (155687404, 155687404, 155687404)

第一季度: 为什么它们的 id() 值相同?既然它们看起来不同,怎么可能是一样的呢?

第二季度: 如何通过 id() 值比较整数列表?

第三季度: 更好奇的是,Python 中的 id() 值是如何计算的?

I found a strange thing in Python when I tried to compare lists composed of integers.

For example:

In [35]: id(range(1,5)),id(range(1,15)),id(range(16,0,-1))
Out[35]: (155687404, 155687404, 155687404)

Q1:
Why their id() values are the same? And how can they be the same since they look different?

Q2:
How can I compare lists of integers by id() values?

Q3:
To be more inquisitive, how is the id() value computed in Python?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

有木有妳兜一样 2024-12-20 13:45:35

直接来自 python 的文档:

Return the “identity” of an object. This is an integer (or long integer) 
which is guaranteed to be unique and constant for this object during its lifetime. 
Two objects with non-overlapping lifetimes may have the same id() value.

您可以获取 md5 哈希值来比较这些对象:

import md5
>>> md5.new(str(range(1,5))).hexdigest()
'd5397571a7f9c05bd58bed77f9dbe8f0'
>>> md5.new(str(range(1,15))).hexdigest()
'000b3ca7f2653a13cdb5b96f21c2ba4d'

Directly from python's doc:

Return the “identity” of an object. This is an integer (or long integer) 
which is guaranteed to be unique and constant for this object during its lifetime. 
Two objects with non-overlapping lifetimes may have the same id() value.

You could get the md5 hash to compare theese objects:

import md5
>>> md5.new(str(range(1,5))).hexdigest()
'd5397571a7f9c05bd58bed77f9dbe8f0'
>>> md5.new(str(range(1,15))).hexdigest()
'000b3ca7f2653a13cdb5b96f21c2ba4d'
南风几经秋 2024-12-20 13:45:35

id 在某种程度上对应于对象的内存位置。您不使用创建的对象,因此它们会被自动删除。当您创建下一个时,它只使用相同的地址。所以你有相同的 id,但它们是不同的对象。

尝试:

>>> x,y,z = range(1,5),range(1,15),range(16,0,-1)
>>> id(x),id(y),id(z)
(36015480, 36015760, 36005368)

The id correspond somehow to the memory location of the object. You don't use the created objects, so as a conseaquence they are automatically deleted. When you create the next one, it just use the same address. So you have the same id, but they are different objetcs.

Try:

>>> x,y,z = range(1,5),range(1,15),range(16,0,-1)
>>> id(x),id(y),id(z)
(36015480, 36015760, 36005368)
我还不会笑 2024-12-20 13:45:35

这是因为在调用 id() 之后,您的范围就超出了范围 - 然后它们的 id 会被重用。

如果它们仍然可以访问,那么它们的 id 将会不同。试试这个:

>>> (a,b,c)=(range(1,5),range(1,15),range(16,0,-1))
>>> (id(a),id(b),id(c))
(3078445292L, 3078088588L, 3078090188L)

This is because just after calling id() your ranges go out of scope - their ids are then reused.

If they were still accessible then their id will be different. Try this:

>>> (a,b,c)=(range(1,5),range(1,15),range(16,0,-1))
>>> (id(a),id(b),id(c))
(3078445292L, 3078088588L, 3078090188L)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文