[SciPy-user] help on array manipulation

Robert Kern robert.kern@gmail....
Wed Nov 21 14:17:26 CST 2007


Tim Michelsen wrote:
> Hello,
> 
> I have the following array:
> 
> In [240]: z
> Out[240]:
> array([[ 15.,   3.,   7.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
>           0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
>           0.,   0.,   0.],
>        [ 16.,   3.,   7.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
>           0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
>           0.,   0.,   0.],
>        [ 17.,   3.,   7.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
>           0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
>           0.,   0.,   0.]])
> 
> How do I get it be become like:
> 
> 15 3
> 15 7
> 15 0
> ...
> 16 3
> 16 7
> ...
> 17 3
> 
> 
> Is there a special function I can use for this or do I need to build various for
> loops to achive this?

import numpy

keys = z[:,0]
values = z[:,1:]
n = values.shape[-1]
z2 = numpy.column_stack([numpy.repeat(keys, n), values.flat])

> I mean this example has few data. But what if the amount increases?
> 
> I am stuck here.
> 
> I'd be very glad if anyone could point be to a good tutorial for array
> manipulation. Scipy Cookbook is not enough.
> Well, I also found :
> http://numpy.scipy.org/numpydoc
> and
> http://www.penzilla.net/tutorials/python/numeric

This helps:
http://www.scipy.org/Numpy_Example_List_With_Doc

but honestly, it mostly comes with practice.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco


More information about the SciPy-user mailing list