0

I have many variables I want to perform the same function on. The return value of the function should be saved in a variable with the the letter m appended to it. I'm currently using a dictionary but I'm having trouble converting the key value pair to a variable name with that pair. I'm listing my approach below and I'm opening to either appending/adding on to this method or using a better method all together

A=4
B=5
C=6
listletter = {'mA':A,'mB':B,'mC':C}
for key in listletter :
    listletter[key]=performfunc(listletter[key])

Note that I do not want to use the dictionary to reference values. My actual use of the variables is in numpy and they're is already alot of indexing going on and the lines are long. I therefore want distinct variable names as opposed to dict['variablename'][][]...

I am aware of the exec function but I here this is bad convention. I'd also appreciate any general advice on simple improvements unrelated to the task(perhaps I should use key, value instead?).

EDIT: I would also be okay with simply performing the function on the variables but not appending the letter m to it. I want to perform the same function on a, b and c. The function would return the new values of a,b and c. Once again I ultimately want to access the variables as a,b & c and not through a dictionary

Cœur
  • 37,241
  • 25
  • 195
  • 267
Seyon
  • 481
  • 6
  • 13
  • Okay, so I just pass my function in for the function argument of map. What exactly do I pass in for the sequence argument? If I create a list of the variables because it's pass by value python would just return an array with all the modified values but the original ones(stored in the variables, not the list) would be the same. Even if it was pass by reference the renaming by appending an m to it wouldn't work right? – Seyon Mar 27 '15 at 03:08
  • 1
    if you "do not want to use the dictionary" than why you show example using one? Please provide example that actually reflects your code and problem. – Marcin Mar 27 '15 at 03:09
  • The dictionary is a means to accomplish my goal. My stated problem stated that I want to perform a function on many variables then save them as the variable name plus the letter m. Using a dictionary is a way for me to specify the variables I want to iterate over. I do not want the final data structure to be a dictionary – Seyon Mar 27 '15 at 03:12

2 Answers2

0

You should be able to use locals() to convert all local variables into a dictionary then prepend m to them in your resulting dictionary.

res = {}
for key, value in locals().items():
   res["m"+key] = somefunc(value)
Kozyarchuk
  • 21,049
  • 14
  • 40
  • 46
  • I do not want to use res to reference the variables each time I use them. I want the variables to be stored as mkey – Seyon Mar 27 '15 at 03:10
  • See http://stackoverflow.com/questions/8028708/dynamically-set-local-variable-in-python for better answer – jkd Mar 27 '15 at 03:10
0

I am not sure how many variables you have. If you have a handful then this should work.

a,b,c,d = 31,21,311,11

print("original values: ", a,b,c,d)

#replace the lambda function with whatever function that you want to use.
a,b,c,d = map(lambda x:x**2,(a,b,c,d))

print("changed values,same variables: ", a,b,c,d)
  • Thanks for the post. I will definitely keep this in mind for future, but I may end up having 10+ variables that I will be changing constantly – Seyon Mar 27 '15 at 03:48