Python对象的数组numpy的 [英] numpy array of python objects

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

问题描述

由于没有numpy的时候允许你定义Python对象的数组? 对象与numpy的阵列。

Since when did numpy allow you to define an array of python objects? Objects array with numpy.

<击>是否有这些数组和Python列表?

什么是这些阵列之间的区别,并说,一个python元组?

What is the difference between these arrays and say, a python tuple?

有几个方便的功能numpy的我想用,即口罩和元素的运算,蟒蛇对象的数组上,我想在我的分析中使用它们,但我很担心使用一个功能,我任何地方无法找到的文档。是否有任何文档,这个对象数据类型?

There are several handy numpy functions I would like to use, i.e. masks and element-wise operations, on an array of python objects and I would like to use them in my analysis, but I'm worried about using a feature I can't find documentation for anywhere. Is there any documentation for this 'object' datatype?

在preparation加入这个功能合并到numpy的标准库?

Was this feature was added in preparation for merging numpy into the standard library?

推荐答案

在根本区别在于numpy的阵列是固定大小的,而一个Python 列表是一个动态数组

The "fundamental" difference is that a Numpy array is fixed-size, while a Python list is a dynamic array.

>>> class Foo:
...  pass
... 
>>> x = numpy.array([Foo(), Foo()])
>>> x.append(Foo())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'numpy.ndarray' object has no attribute 'append'

(你可以绕过这与 numpy.concatenate ,但仍numpy的阵列并不意味着作为一个下拉更换为列表

(You can get around this with numpy.concatenate, but still Numpy arrays aren't meant as a drop-in replacement for list.)

对象是非常清楚的记载,但要注意,你必须通过 DTYPE =对象有时:

Arrays of object are perfectly well documented, but be aware that you'll have to pass dtype=object sometimes:

>>> numpy.array(['hello', 'world!'])
array(['hello', 'world!'], 
      dtype='|S6')
>>> numpy.array(['hello', 'world!'], dtype=object)
array(['hello', 'world!'], dtype=object)

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

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