[Numpy-discussion] Array of Arrays

Berthold Höllmann berthold@xn--hllmanns-n4a...
Fri Mar 23 14:26:15 CDT 2007


"Alexander Michael" <lxander.m@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