[Numpy-discussion] Suppressing "nesting" (recursion, descent) in array construction.

Francesc Altet faltet at carabos.com
Thu Jun 21 03:43:52 EDT 2007


El dc 20 de 06 del 2007 a les 12:43 -0700, en/na Michael McNeil Forbes
va escriure:
> Hi,
> 
> That is a little more complicated than I want, but it shows me the  
> solution: Construct the array of the desired shape first, then fill it.
> 
> data = [1.0, 3,0]
> keys = [('a',1),('b',2)]
> 
> # Convert to arrays for indexing
> data_array = array(data1)
> key_array = empty(len(keys),dtype=tuple)
> key_array[:] = keys[:]

the later two statements can also be written as:

key_array = array(keys, dtype=tuple)

> inds = argsort(data)
> data_array[:] = data[inds]
> key_array[:] = keys[inds]

Yeah, much simpler than my first approach.

Cheers,

-- 
Francesc Altet    |  Be careful about using the following code --
Carabos Coop. V.  |  I've only proven that it works, 
www.carabos.com   |  I haven't tested it. -- Donald Knuth




More information about the NumPy-Discussion mailing list