[Numpy-discussion] array of tuples

Stefan van der Walt stefan at sun.ac.za
Tue Jun 6 16:01:14 CDT 2006


On Tue, Jun 06, 2006 at 01:05:43PM -0500, Travis N. Vaught wrote:
> looping).  Is there a quick way to do this with dtype?
> 
> I've tried:
> 
>  >>> import numpy
>  >>> x = [(1,2,3),(4,5,6)]
>  >>> numpy.array(x)
> array([[1, 2, 3],
>        [4, 5, 6]])
>  >>> numpy.array(x, dtype='p')
> array([[1, 2, 3],
>        [4, 5, 6]])
>  >>> numpy.array(x, dtype='O')
> array([[1, 2, 3],
>        [4, 5, 6]], dtype=object)

It works if you pre-allocate the array:

In [18]: x = [(1,2),(3,4)]

In [19]: z = N.empty(len(x),dtype='O')

In [20]: z[:] = x

In [21]: z
Out[21]: array([(1, 2), (3, 4)], dtype=object)

Regards
Stéfan




More information about the Numpy-discussion mailing list