arrays - Function returning elements of a multi-dimensonal list in python -


i trying define function returns elements of multi-dimensional variable according nested list of indices. working variables depending on multiple dimensions like, e.g.: time, altitude, latitude, longitude.

make 3d (e.g., time, altitude , latitude):

x = np.arange(125).reshape(5,5,5) 

if want first 4 time steps, first 3 altitude levels , first 2 latitudes can several things:

x[[0,1,2,3],:,:][:,[0,1,2],:][:,:,[0,1]] 

or

x[ [[[0]],[[1]],[[2]],[[3]]],[[0],[1],[2]],[0,1]] 

or

x[np.ix_([0,1,2,3],[0,1,2],[0,1])] 

but have function giving me elements

def get_elements( x, l ) 

where l list of indices

l = [[0,1,2,3],[0,1,2],[0,1]] 

how function like? last alternative comes pretty close x[np.ix_(l)] gives me indexerror.

moreover, great have opportunity leave dimensions untouched. e.g., using time steps in pseudo-code:

l = [[:],[0,1,2],[0,1]] 

thanks lot!

note signature of np.ix_:

np.ix_(*args) 

so need 'expand' l:

x[np.ix_(*l)] 

take @ code ix_. notice things iterate on args, , returns tuple. @ other functions in np.lib.index_tricks.

in general, if list gives problems when indexing, check whether need tuple instead. familiarize slice.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -