[Tutor] array manipulations

Lie Ryan lie.1296 at gmail.com
Thu Jun 4 06:35:54 CEST 2009


NTB wrote:
> Hello,
> I'm an R user, who just started with numpy.  I'm planning on setting up
> a large nutrient database.  Here's my question:
>  
> I'm trying to reshape? or manipulate? the following array:
>  
>   array [['Food 1', 'Nutrient 1', 0.9],
>             ['Food 1', 'Nutrient 2', 0.2],
>             ['Food 2', 'Nutrient 1', 0.55],
>             ['Food 2', 'Nutrient 2', 0.11]]
>  
> into a new array that looks like this:
>  
> array [['Food 1', 0.9, 0.2],
>          ['Food 2', 0.55, 0.11]]
>  
> or more simply:
>  
> array [[0.9, 0.2],
>          [0.55, 0.11]]
>  
> In other words, the data and shape that I need in the first array is the
> 3rd column, where the row names are the 1st column and the column names
> are the 2nd column.
>  
> I am thinking that I need to use .flat(), maybe with some kind of list
> comprehension?  I really don't know, and would really appreciate some help.
>  

Use itertools.groupby() from the itertools module

>>> arr = [['Food 1', 'Nutrient 1', 0.9],
...        ['Food 1', 'Nutrient 2', 0.2],
...        ['Food 2', 'Nutrient 1', 0.55],
...        ['Food 2', 'Nutrient 2', 0.11],
...       ]
>>> grouped = itertools.groupby(arr, lambda x: x[0])
>>> for name, food in grouped:
...     print name
...     print [nutrient[2] for nutrient in food]
...
Food 1
[0.90000000000000002, 0.20000000000000001]
Food 2
[0.55000000000000004, 0.11]

you may need to sort the array first since groupby only group adjacent
enties. The sorted() and list.sort() function accept a key= argument
that would be a function/lambda that generates the key to sort with

sorted(arr, key=labmda x: x[0])
or
arr.sort(key=lambda x: x[0])



More information about the Tutor mailing list