I have three indexes, x,y,t
and a tridimensional matrix (it's actually a netcdf variable) in python but the order in which the indexes have to be applied to the matrix change. So, to make it easily user-definable I am trying to get the specific element I want as
order='t,x,y' # or 't,y,x' or anything like this
elem=matrix[eval(order)]
but this fails with TypeError: illegal subscript type
. When I try
a=eval(order)
print type(a)
it gives me that a
is a tuple, so I'm guessing this is the source of my problem. But why is a
a tuple? Any ideas as how to do this? Documentation wasn't helpful.
Also, somehow doing
a=eval(order)
i,j,k=a
elem=matrix[i,j,k]
doesn't work either. Not sure as to why.
EDIT
People are misunderstanding what I'm trying to do here apparently, so let me explain a little better. This is inside a function where the values x
, y
, t
are already defined. However, the order in which to apply those indexes should be provided by the user. So the call would be something like func(order='t,x,y')
. That's at least the only way I figured the user could pass the order of the indexes as a parameter. Sorry for the confusion.