[Numpy-discussion] Array of Arrays

Berthold Höllmann berthold at xn--hllmanns-n4a.de
Fri Mar 23 15:26:15 EDT 2007


"Alexander Michael" <lxander.m at gmail.com> writes:

> How can I do something like the following?
>
> a = empty((5,7), dtype=<4 element array of floats>)
>
> c = a[:,-1] # last column of 4 element arrays
>
> a[0,0] = 2.0
> print a[0,0]
> [2. 2. 2. 2.]
>
> a[1,0] = 3.0
> a[0,1] = a[0,0] * a[1,0]
>
> print a[0,1]
> [6. 6. 6. 6.]

The nearest I could come with is::

.>>> import numpy
.>>> a = numpy.zeros((5,7), numpy.dtype('object'))
.>>> a[:,:]=None
.>>> class O:
....     def __init__(self, val):
....         self.val = val
....     def __mul__(self, other):
....         return self.__class__(self.val*other.val)
....     def __str__(self):
....         return "%s" % (numpy.ones(4, numpy.dtype('float')) * self.val)
.... 
.>>> a[0,0] = O(2.0)
.>>> print a[0,0]
[ 2.  2.  2.  2.]
.>>> a[1,0] = O(3.0)
.>>> a[0,1] = a[0,0] * a[1,0]
.>>> print a[0,1]
[ 6.  6.  6.  6.]

Regards
-- 



More information about the NumPy-Discussion mailing list