Saturday, 31 August 2013

Checking for multiple strings in python in a dictionary

Checking for multiple strings in python in a dictionary

I have a dictionary:
mot={"READ":["0001",2],"MOVER":["0010",4],"MOVEM":["0011",2],"ADD":["0101",1],"COMP":["0110",2],"BC":["0111",3],"PRINT":["1000",5],"STOP":["1001",8]}
It consists of an instruction followed by its code in binary and the
amount of bytes it takes.
I read an input line from a file and store it in the variable str2. I want
to check if any of the strings in the above dictionary occur in str2.
Now, I am using this code to check if that happens:
if any(x in str2 for x in mot):
#do something
Now, my problem is: I have a location variable. In place of do something I
want to write:
location=location+mot[x][1]
That is, I want to increment the location variable based on which x is
found. How do I do this? If I do it in the above manner, it gives me an
error saying x is not defined.

No comments:

Post a Comment