Python numpy数据指针地址无需更改即可更改 [英] Python numpy data pointer addresses change without modification

查看:67
本文介绍了Python numpy数据指针地址无需更改即可更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑

经过一些摆弄之后,到目前为止,我已经隔离了以下状态:

After some more fiddling around, I've so far isolated the following states:

  1. 一个一维数组在直接输入变量时提供两个不同的地址,而在使用print()
  2. 时仅提供一个
  3. 一个二维数组(或矩阵)在直接输入变量时提供三个不同的地址,在两个时使用print()
  4. 3D数组在直接输入变量时会给出两个不同的地址,而在使用print()时只能给出一个(显然与一维数组)
  1. A 1D array gives two different addresses when entering variable directly, and only one when using print()
  2. A 2D array (or matrix) gives three different addresses when entering variable directly, and two when using print()
  3. A 3D array gives two different address when entering variable directly, and only one when using print() (apparently the same as with the 1D array)

像这样:

>>> a = numpy.array([1,2,3], dtype="int32")

>>> a.data
<memory at 0x7f02e85e4048>
>>> a.data
<memory at 0x7f02e85e4110>
>>> a.data
<memory at 0x7f02e85e4048>
>>> a.data
<memory at 0x7f02e85e4110>
>>> a.data
<memory at 0x7f02e85e4048>

>>> print(a.data)
<memory at 0x7f02e85e4110>
>>> print(a.data)
<memory at 0x7f02e85e4110>
>>> print(a.data)
<memory at 0x7f02e85e4110>
>>> print(a.data)
<memory at 0x7f02e85e4110>
>>> print(a.data)
<memory at 0x7f02e85e4110>


>>> d = numpy.array([[1,2,3]], dtype="int32")

>>> d.data
<memory at 0x7f02e863ae48>
>>> d.data
<memory at 0x7f02e863a9e8>
>>> d.data
<memory at 0x7f02e863aac8>
>>> d.data
<memory at 0x7f02e863ae48>
>>> d.data
<memory at 0x7f02e863a9e8>
>>> d.data
<memory at 0x7f02e863aac8>

>>> print(d.data)
<memory at 0x7f02e863ae48>
>>> print(d.data)
<memory at 0x7f02e863a9e8>
>>> print(d.data)
<memory at 0x7f02e863ae48>
>>> print(d.data)
<memory at 0x7f02e863a9e8>
>>> print(d.data)
<memory at 0x7f02e863ae48>


>>> b = numpy.matrix([[1,2,3],[4,5,6]], dtype="int32")

>>> b.data
<memory at 0x7f02e863a9e8>
>>> b.data
<memory at 0x7f02e863ae48>
>>> b.data
<memory at 0x7f02e863aac8>
>>> b.data
<memory at 0x7f02e863a9e8>
>>> b.data
<memory at 0x7f02e863ae48>

>>> print(b.data)
<memory at 0x7f02e863aac8>
>>> print(b.data)
<memory at 0x7f02e863a9e8>
>>> print(b.data)
<memory at 0x7f02e863aac8>
>>> print(b.data)
<memory at 0x7f02e863a9e8>
>>> print(b.data)
<memory at 0x7f02e863aac8>


>>> c = numpy.matrix([[1,2,3],[4,5,6],[7,8,9]], dtype="int32")

>>> c.data
<memory at 0x7f02e863aac8>
>>> c.data
<memory at 0x7f02e863a9e8>
>>> c.data
<memory at 0x7f02e863ae48>
>>> c.data
<memory at 0x7f02e863aac8>
>>> c.data
<memory at 0x7f02e863ae48>
>>> c.data
<memory at 0x7f02e863a9e8>
>>> c.data
<memory at 0x7f02e863aac8>

>>> print(c.data)
<memory at 0x7f02e863ae48>
>>> print(c.data)
<memory at 0x7f02e863a9e8>
>>> print(c.data)
<memory at 0x7f02e863ae48>
>>> print(c.data)
<memory at 0x7f02e863a9e8>
>>> print(c.data)
<memory at 0x7f02e863ae48>


>>> e = numpy.array([[[0,1],[2,3]],[[4,5],[6,7]]], dtype="int32")

>>> e.data
<memory at 0x7f8ca0fe1048>
>>> e.data
<memory at 0x7f8ca0fe1140>
>>> e.data
<memory at 0x7f8ca0fe1048>
>>> e.data
<memory at 0x7f8ca0fe1140>
>>> e.data
<memory at 0x7f8ca0fe1048>


>>> print(e.data)
<memory at 0x7f8ca0fe1048>
>>> print(e.data)
<memory at 0x7f8ca0fe1048>
>>> print(e.data)
<memory at 0x7f8ca0fe1048>


原始帖子

给我的印象是,仅在python控制台中输入一个变量,然后回显一个仅描述其值(和类型)的字符串.它的格式与print()的格式不同,但是我假设它们都返回相同的值.

I was under the impression that merely entering a variable along in the python console with echo a string simply describing the value (and type) of it. It formats in a different manner than print(), but I assumed the values they both returned would be the same.

当我尝试输出numpy对象的数据指针对象的地址时,每隔一次输入变量都会给我不同的值,而print()会给出相同的值.

When I try to output the address of the data pointer object of a numpy object, just entering the variable gives me different value every other time, while print() gives the same value.

这表明这两个操作的区别不仅在于输出的格式如何,还在于它们从何处获取信息.但是这些额外的差异到底由什么构成?

That suggests that the difference in the two operations aren't just how the output is formatted, but also where they get their information from. But what exactly do these additional differences consist of?

>>> a = numpy.array([0,1,2])

>>> a
array([0, 1, 2])
>>> print(a)
[0 1 2]

>>> print(a.data)
<memory at 0x7ff25120c110>
>>> print(a.data)
<memory at 0x7ff25120c110>
>>> print(a.data)
<memory at 0x7ff25120c110>

>>> a.data
<memory at 0x7ff25120c110>
>>> a.data
<memory at 0x7ff253099818>
>>> a.data
<memory at 0x7ff25120c110>
>>> a.data
<memory at 0x7ff253099818>
>>> a.data
<memory at 0x7ff25120c110>

推荐答案

a.data返回的memoryview似乎在两个(或更多)视图之间交替.如果存储给定的a.data实例,则会得到一致的输出:

The memoryview returned by a.data seems to alternate between two (or more) views. If you store a given instance of a.data, you get consistent output:

>>> a.data
<memory at 0x7fb88ea1f828>
>>> a.data
<memory at 0x7fb88e98c4a8>
>>> t = a.data
>>> a.data
<memory at 0x7fb88e98ce48>
>>> a.data
<memory at 0x7fb88e98c3c8>
>>> a.data
<memory at 0x7fb88e98c4a8>
>>> a.data
<memory at 0x7fb88e98ce48>
>>> a.data
<memory at 0x7fb88e98c3c8>
>>> a.data
<memory at 0x7fb88e98c4a8>
>>> t
<memory at 0x7fb88ea1f828>
>>> t
<memory at 0x7fb88ea1f828>
>>> t
<memory at 0x7fb88ea1f828>

请注意,在上面的示例中有3个地址轮换;我很确定这都是实现细节.我猜想其中涉及到一些缓存,这意味着每次访问a.data时实际上并不会生成新视图.

Note that there are 3 addresses rotating in the above example; I'm pretty sure this is all an implementation detail. I would guess that some caching is involved, implying that a new view is not actually generated each time you access a.data.

您还可以确保您正在查看单独的视图对象:

You can also make certain that you are looking at separate view objects:

>>> id(a.data)
140430643088968
>>> id(a.data)
140430643086280
>>> id(a.data)
140430643088968
>>> id(a.data)
140430643086280

因此,大多数困惑可能来自以下事实:a.data的属性符号会暗示它是我们正在讨论的固定对象,而事实并非如此.

So most of the confusion probably comes from the fact that the attribute notation of a.data would suggest that it's a fixed object we're talking about, while this is not the case.

这篇关于Python numpy数据指针地址无需更改即可更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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