I have 30 variables with the pattern aod7039, aod7040, ...., aod7068.
I want to do the same operation (i.e. calculate the mean over an axis) for all these variables and overwrite the original variables.
Up to now I wrote 30 times the same line, and wondered if there isn't maybe an shorter and easier way to do this?
I am gradeful for every idea!
5 Answers
I have 30 variables with the pattern aod7039, aod7040, ...., aod7068
Then you have a design problem - you should have a list or dict instead. Replace all those variables with either a list or dict (or collections.OrderedDict if you need key access while preserving insertion order) and then it's only a matter of iterating over your container, ie
# with a list:
for index, item in enumerate(yourlist):
yourlist[index] = do_something_with(item)
# with a dict or OrderedDict:
for key, item in enumerate(yourdict):
yourdic[key] = do_something_with(item)
evalbut keep that in mind eval is evil