1

I have a bit of code that I want to run multiple times. That seams trivial but there is a twist: I want to change the code in a specific way between iterations. For example:

A = 1
B = ['+','-','/'.'*','**']
C = []
for x in range(len(B)):
    C.append(A{B[x]}100)
print(C)

Now, I know this code doesn't work and it's not a proper Python syntax, but i't just an example of what I'd like the code to do.

Ideally I'd get C as a list where 0th element is 1 + 100, 1st element is 1 - 100, 2nd element is 1 / 100 etc. (N.b.: NOT '1 + 100' string. A result of 1 + 100 calculation - 101). Basically I want the code to change itself between iterations of loop in a defined way.

I do not want to define some lengthy if/elif statement since list B is very, very long.

Edit:

Let me give another example. This one is more relevant to my problem.

A = ['mom','dad','me','you','c']
B = ['a','b','something','nothing','cat']
for x in range(len(A)):
    C_{A[x]} = B[x]

I want to end up with 5 new variables so that:

Print(C_mom)
a
Print(C_dad)
b
Print(C_c)
cat

Again, I recognize this is not a proper python syntax and this code doesn't work.

MarcinKonowalczyk
  • 2,577
  • 4
  • 20
  • 26

1 Answers1

2

First create a dict where each string '+','*' etc point to it's corresponding method imported from operator module.

Now loop over B and fetch the corresponding method from the ops dict and pass the operands to the method.

>>> from operator import add,sub,mul,div,pow
>>> ops = {'+':add,'-':sub,'/':div, '*':mul,'**':pow}
>>> B = ['+','-','/','*','**']
>>> A = 1
>>> [ops[item](A,100) for item in B]
[101, -99, 0, 100, 1]

Use '/': operator.truediv if you want ops['/'](1,100) to return 0.01 instead of 0.

Update:

Creating dynamic variables in python is not a good idea, you should better use a dict here:

>>> A = [1,2,3,4,5]
>>> B = ['a','b','something','nothing','cat']
>>> c = {x:y for x,y in zip(A,B)}
>>> c[1]
'a'
>>> c[2]
'b'
>>> c[5]
'cat

Use globals() to create dynamic variables(don't use this method):

for x,y in zip(A,B):
    globals()['C'+str(x)] =y
...     
>>> C1
'a'
>>> C2
'b'
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • Or you could just use `B = [add, sub, div, mul, pow]` directly and avoid the extra indirection. Also the OP says B is very long, so presumably it must include other user-defined functions rather than just simple operators. – Duncan Jul 02 '13 at 10:58
  • @AshwiniChaudhary Thank you. It's not compleate solution to my problem though. (See edit) – MarcinKonowalczyk Jul 02 '13 at 11:04
  • @447xpro I've updated my solution, I guess that's what you wanted. – Ashwini Chaudhary Jul 02 '13 at 11:08
  • I'd probably use `dict(zip(A, B))` instead (although I admit you can't perform transformations as easily that way). You're definitely right that dictionaries are much better than the usual hacks to make dynamic variables. – DSM Jul 02 '13 at 11:12
  • @AshwiniChaudhary Again, no. You are using __getitem__ form list C. I want a variable which's name is C1. A could be a list of strings `A = ['car','bike',...]` and I'd need to get Ccar = a, Cbike = b etc. (I've edited the post again to avoid this confusion) – MarcinKonowalczyk Jul 02 '13 at 11:12
  • @447xpro I know what you want, but creating dynamic variables like that is not a good idea. Prefer dict over that. – Ashwini Chaudhary Jul 02 '13 at 11:14
  • @AshwiniChaudhary Why is it not a good idea? Even if it is so and one shouldn't use it, I'd like to learn it nevertheless and the never use it again just for the sake of knowledge. – MarcinKonowalczyk Jul 02 '13 at 11:16
  • @AshwiniChaudhary, Thank you a lot. :) I will do as you advise and not use the last method. It's good to know though. – MarcinKonowalczyk Jul 02 '13 at 11:20
  • @447xpro http://stackoverflow.com/questions/17167591/creating-a-list-based-on-value-stored-in-variable-in-python/17167649#17167649 – Ashwini Chaudhary Jul 02 '13 at 11:22