numpy数组更改id [英] Numpy arrays changing id

查看:103
本文介绍了numpy数组更改id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是怎么回事?似乎数组的id位置不能保持稳定,也许是因为即使id是相同的,操作员还是返回False.然后在打印数组后,元素的id就会改变.有什么解释吗?

What is happening here? It seems like the id locations of the array is not remaining steady maybe?is operator is returning False even thought the ids are same. then after printing the arrays the ids of elements are changing. Any explanations?

import numpy as np
a = np.arange(27)
b = a[1:5]
a[0] is b[1] #False
id(a[0]) #40038736L
id(b[1]) #40038736L
a #prints the array
id(b[1]) #40038712L
id(a[0]) #40038712L
b[0] #1
a[1] #1
id(b[0]) #40038712L
id(a[1]) #40038784L

推荐答案

第一个带有列表的测试:

First test with a list:

In [1109]: a=[0,1,2,3,4]
In [1112]: b=a[1:3]

In [1113]: id(a[1])
Out[1113]: 139407616
In [1114]: id(b[0])
Out[1114]: 139407616

In [1115]: a[1] is b[0]
Out[1115]: True

后来我尝试了

In [1129]: id(1)
Out[1129]: 139407616

所以a[1]中的对象始终是整数1(整数的id有点棘手,并且取决于实现).

So the object in a[1] is consistently the integer 1 (id of integers is a bit tricky, and implementation dependent).

但是带有数组:

In [1118]: aa=np.arange(5)
In [1119]: ba=aa[1:]

In [1121]: aa[1]
Out[1121]: 1
In [1122]: ba[0]
Out[1122]: 1
In [1123]: id(aa[1])
Out[1123]: 2925837264
In [1124]: id(ba[0])
Out[1124]: 2925836912

id完全不同;实际上,它们随着每次访问而改变:

id are totally different; in fact they change with each access:

In [1125]: id(aa[1])
Out[1125]: 2925837136
In [1126]: id(ba[0])
Out[1126]: 2925835104

这是因为aa[1]不只是整数1.这是一个np.int32对象.

That's because aa[1] isn't just the integer 1. It is a np.int32 object.

In [1127]: type(aa[1])
Out[1127]: numpy.int32

与列表相反,数组的值作为字节存储在databuffer中. b[1:]view,并访问相同的数据缓冲区.但是a[1]是一个新对象,其中包含对该数据缓冲区的引用.与列表情况相反,a[1]不是a中的第二个对象.

In contrast to a list, values of an array are stored as bytes in a databuffer. b[1:] is a view and accesses the same data buffer. But a[1] is a new object that contains a reference to that data buffer. In contrast to the list case, a[1] is not the 2nd object in a.

通常,id在使用数组时没有用,并且is测试也没有用.使用==isclose(对于浮点数).

In general, id is not useful when working with arrays, and the is test is also not useful. Use == or isclose (for floats).

================

================

查看aa的值存储位置的方法是:

A way to see where the values of aa are stored is with:

In [1137]: aa.__array_interface__
Out[1137]: 
{'data': (179274256, False),      # 'id' so to speak of the databuffer
 'descr': [('', '<i4')],
 'shape': (5,),
 'strides': None,
 'typestr': '<i4',
 'version': 3}
In [1138]: ba.__array_interface__
Out[1138]: 
{'data': (179274260, False),    # this is 4 bytes larger
 'descr': [('', '<i4')],
 'shape': (4,),
 'strides': None,
 'typestr': '<i4',
 'version': 3}

两个数组的data指针相关,因为baview.

the data pointer for the 2 arrays is related because ba is a view.

aa[1]类似于数组,并且也具有数据缓冲区,但它不是视图.

aa[1] is array-like, and too has a data buffer, but it isn't a view.

In [1139]: aa[1].__array_interface__
Out[1139]: 
{'__ref': array(1),
 'data': (182178952, False),
 ...}

这篇关于numpy数组更改id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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