1

I have a condition where user I have a[]. P is a file which has all the variables l, m, n .... which are populated.

then

if a[1] = '1': p.l = a[2] elif a[1] = '2': p.m = a[2] elif a[1] = '3': p.n = a[2] 

these conditions keep going till 20-25 of them

What is the best way to handle this in both python 3.6 and python 2.7

Additional info

Hi, Sorry for the undetailed explanation. l,m,n,etc there are many other variables . But i will have to define them as and when needed. how every 1,2,3, and etc there might be upto 10-15 , it depends , but they are fixed as in those numbers will not change, but l,m,n are something I defined so can be modified.

Point is suppose I get a string like "Hi how are you" and "Hello how are you" then I put this string in variable say 'a'. then I split the string to a[] = ['Hi','How','Are','You'] and a[] = ['Hello','How,'Are','you']. Two separate times Now I read the

if a[1] = 'HI': p.l = a[2] elif a[1] = 'Hello': p.m = a[2] elif a[1] = 'How': p.n = a[2] 

So here depending on a[1] value I need to store the a[2] value in different variable. Also a[1] might have 20-25 different inputs comming. depending on the input I will have to store it in that particular variable.

3
  • are there only those 3 variables l, m, n or does the P file have 20-25 more different variables? If only the 3 variables l, m, and n, how are the next, few subsequent variables in the 20-25 defined? Do l, m, and n recycle for '4', '5' and '6', etc? Commented May 24, 2017 at 20:33
  • Hi, Sorry for the undetailed explanation. Commented May 25, 2017 at 16:48
  • Hi, that's okay. Only I didn't understand, maybe the answers below do what you are asking. Commented May 25, 2017 at 16:52

2 Answers 2

2

You can use a dictionary to hold the names of the attributes together with setattr to set the attribute.

lookup = {'1': 'l', '2': 'm', '3': 'n'} key, value = a[1], a[2] attribute = lookup[key] setattr(p, attribute, value) 
Sign up to request clarification or add additional context in comments.

1 Comment

A map in Python is called a dictionary. Still a good answer though.
0

For things like this, I like to use a dict as a sort of dispatch table.

You can define a map that looks like this:

set_map = { '1': "l", '2': "m", '3': "n" } 

and then encode your conditional logic as a dictionary lookup:

setattr(p, set_map[a[1]], a[2]) 

setattr is short of 'set attribute'. It takes as arguments an object, an attribute name, and a value.

The way it works is by first resolving the values of a and then resolving the values of set_map[a[1]], which will be one of our letters ('l', 'm', or 'n'). It then sets the property of the object with the value.

For an example, assume that a[1] = '1' and a[2] = 5, our dictionary lookup would yield

set_map[a[1]] == set_map['1'] == 'l' 

which then setattr would resolve to

setattr(p, 'l', 5) 

which is equivalent of typing

p.l = 5 

15 Comments

That assigns to the set but not to p.x.
Thanks for the catch. I usually do with with function pointers, it didn't translate to attributes as fluidly.
Can you please explain what it is doing in the above code. Thank you Rocky
I've added some more explanation in the answer body. please let me know if this helps. Is there anything else I could explain?
Why is a[2] = 5 isn't it equal to 'm'? a = ['1','2','3'] p = '' b = ['Hi','Hello'] set_map = { '1': "l", '2': "m", '3': "n" } print('a[2] is:',a[2]) print('set_map[a[1]] is:',set_map[a[1]]) setattr(p, set_map[a[1]], b[1]) Result a[2] is: 3 set_map[a[1]] is: m Traceback (most recent call last): setattr(p, set_map[a[1]], b[1]) AttributeError: 'str' object has no attribute 'm' Process finished with exit code 1 Please correct me.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.