Skip to content Skip to sidebar Skip to footer

Numpy Array Of Python Objects

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 t

Solution 1:

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

>>>classFoo:...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)

Post a Comment for "Numpy Array Of Python Objects"