[Numpy-discussion] array of tuples

David M. Cooke cookedm at physics.mcmaster.ca
Tue Jun 6 16:07:05 CDT 2006

On Tue, 06 Jun 2006 13:21:56 -0700
Christopher Barker <Chris.Barker at noaa.gov> wrote:

> Travis N. Vaught wrote:
> > I'd like to construct an array of tuples and I'm not sure how (without 
> > looping). 
> Is this what you want?
>  >>> import numpy as N
>  >>> a = N.empty((2,),dtype=object)
>  >>> a[:] = [(1,2,3),(4,5,6)]
>  >>> a
> array([(1, 2, 3), (4, 5, 6)], dtype=object)
>  >>> a.shape
> (2,)
> By the way, I notice that the object dtype is not in the numpy 
> namespace. While this mikes sense, as it's part of python, I keep 
> getting confused because I do need to use numpy-specific dtypes for 
> other things. I never use import *, so it might be a good idea to put 
> the standard objects dtypes in the numpy namespace too. Or maybe not, 
> just thinking out loud.

None of the Python types are (int, float, etc.). For one reason, various
Python checkers complain about overwriting a builtin type, and plus, I
think it's messy and a potential for bugs. numpy takes those as convenience
types, and converts them to the appropriate dtype. If you want the dtype
used, it's spelled with an appended _.

So in this case you'd want dtype=N.object_. N.object0 works too.

|David M. Cooke                      http://arbutus.physics.mcmaster.ca/dmc/
|cookedm at physics.mcmaster.ca

More information about the Numpy-discussion mailing list