Cannot store Sequences in Numeric arrays of Objects

Konrad Hinsen hinsen at cnrs-orleans.fr
Mon Jun 7 10:43:35 CEST 1999


Stuart I Reynolds <S.I.Reynolds at cs.bham.ac.uk> writes:

> >>>#Cannot store an array
> >>> a[0,0] = b
> Traceback (innermost last):
>   File "<stdin>", line 1, in ?
> ValueError: array too large for destination

NumPy handles assignments from sequences to arrays correctly in the
"standard" cases, but unfortunately this makes it difficult to assign
sequences to individual elements of general object arrays (which
aren't that well supported anyway).

However, you can assign sequences to elements of one-dimensional arrays:

>>> from Numeric import *
>>> a = zeros((5,), PyObject)
>>> a[0] = [1, 2, 3]
>>> a[1] = ['a', ['b', 'c']]
>>> a
array([[1, 2, 3] , ['a', ['b', 'c']] , 0 , 0 ,
            0 ],'O')
>>> a[2:4] = ['foo', 'bar']
>>> a[4] = array(3*[None], PyObject)
>>> a
array([[1, 2, 3] , ['a', ['b', 'c']] , foo ,
            bar , [None  None  None ] ],'O')

In your case, a[0][0] = b will do what you want. I don't know why
assignment to higher-dimensional arrays fails, but I don't really want
to figure it out either!
-- 
-------------------------------------------------------------------------------
Konrad Hinsen                            | E-Mail: hinsen at cnrs-orleans.fr
Centre de Biophysique Moleculaire (CNRS) | Tel.: +33-2.38.25.55.69
Rue Charles Sadron                       | Fax:  +33-2.38.63.15.17
45071 Orleans Cedex 2                    | Deutsch/Esperanto/English/
France                                   | Nederlands/Francais
-------------------------------------------------------------------------------




More information about the Python-list mailing list