Basically I have a list of key:items in a dictionary, and I am using a loop to find the key that matches the item I get, and I have functions for every key. The keys are numbered though, so I need to take off the last number.
This leaves me with needing to call a function from a string like the one in the subject:
key[-1]()
key[-1] actually is a string that returns something like scalcs, and that is the name of a function of mine that I then need to call.
I’ve searched around and have seen stuff about classes. I’m new and honestly have never worked with classes at all and have a lot of code and a lot of these functions. I would rather not do that method if at all possible.
d = {key1: item1, key2: item2, akey1: item3, akey2: item4, dkey1: item1, dkey2: item2}
I want to call key, akey, or dkey when my value matches that of the value attached to the key in the dictionary.
So I will loop through d like this:
for key, value in d.items():
if id == value:
print key[:-1]
Now I just need to call that key[:-1]
1 Answer