且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从NumPy中的对象数组获取属性

更新时间:2023-01-17 13:37:23

与您想要的内容最接近的是使用recarray而不是Python对象的ndarray:

The closest thing to what you want is to use a recarray instead of an ndarray of Python objects:

num_stars = 10
dtype = numpy.dtype([('x', float), ('y', float), ('colour', float)])
a = numpy.recarray(num_stars, dtype=dtype)
a.colour = numpy.arange(num_stars)
print a.colour

打印

[ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9.]

使用Python对象的NumPy数组通常比使用普通的list效率低,而recarray则以更有效的格式存储数据.

Using a NumPy array of Python objects usually is less efficient than using a plain list, while a recarray stores the data in a more efficient format.