[Tutor] Extract several arrays from a large 2D array

Alan Gauld alan.gauld at btinternet.com
Thu Jan 21 05:05:00 EST 2016


On 21/01/16 04:22, Ek Esawi wrote:

> I have a 2D array (2000, 4); an example is given abelow.  On the 1st column
> I have 15 variables, on the 2nd 4 variables. 

Your terminology is a little confusing since variables in
Python normally means names. You have values in your columns.
So I suspect when you say variables you mean distinct values?

> want a code that generate 4 arrays for each variable on the 1st column;
> each array contains all the values from column 4. Then I want to find the
> average of each array.

This is very different to what you describe later in your example.

> For example; pick the 1st variable in column 1 which is 1; then pick the 1st
> variable on the 2nd column which is 5. Now I want an array that contains
> all the values on the 4th column that match variable 1 on the 1st and
> variable 5 on the 2nd column. I need to get the average of each array.

In this particular case you are describing a tuple key (colA,colB)
There may be more efficient solutions, but I would create a dictionary
using the tuple as key and appending the values on colD to a list.
You can then take the average of the list for each tuple.

So in pseudo code:

data = {}
for row in myArray:
    data.get((row[0],row[1]),[]).append row[3]

for key in data:
    print key,':',sum(data[key]/len(data[key])



HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list