开发者

numpy array of python objects

开发者 https://www.devze.com 2023-03-08 10:02 出处:网络
Since when did numpy allow you to define an array of python objects? Objects array with numpy. Is there any fundamental difference between these arrays and a python list?

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

Is there any fundamental difference between these arrays and a python list?

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

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?

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


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'

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

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消