Using Numeric arrays to store different datatypes

Phil Austin paustin at eos.ubc.ca
Thu Nov 22 12:52:37 EST 2001


Rajarshi Guha <rxg218 at psu.edu> writes:

> Hi,
>   Is it possible to store say a list variable in a Numeric array?
> Or even a class in a Numeric arry?
> 

use Numeric.PyObject, but note there's a bug with
multidimensional indexing with this type:

>>> testArray=Numeric.zeros(16,Numeric.PyObject)
>>> testArray[10]=[1,2,3.4]
>>> testArray
array([0 , 0 ,
            0 , 0 ,
            0 , 0 ,
            0 , 0 ,
            0 , 0 ,
            [1, 2, 3.3999999999999999] , 0 ,
            0 , 0 ,
            0 , 0 ],'O')

>>> testArray.shape
(16,)
>>> testArray.shape=(4,4)
>>> testArray
array([[0 , 0 ,
             0 , 0 ],
       [0 , 0 ,
             0 , 0 ],
       [0 , 0 ,
             [1, 2, 3.3999999999999999] , 0 ],
       [0 , 0 ,
             0 , 0 ]],'O')
>>> testArray[1,1]=[1,2,3]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: array too large for destination
>>> testArray.flat[2]=[1,2,3,4]
>>> testArray
array([[0 , 0 ,
             [1, 2, 3, 4] , 0 ],
       [0 , 0 ,
             0 , 0 ],
       [0 , 0 ,
             [1, 2, 3.3999999999999999] , 0 ],
       [0 , 0 ,
             0 , 0 ]],'O')



More information about the Python-list mailing list