I have a dictionary like this
dictionary = {"1":"abc_123","2":"def_456" "3":"ghi_789"}
so I want to convert "abc_123"
as a abc_123
because I have a function call abc_123
, can anyone help me please.
I have a dictionary like this
dictionary = {"1":"abc_123","2":"def_456" "3":"ghi_789"}
so I want to convert "abc_123"
as a abc_123
because I have a function call abc_123
, can anyone help me please.
# You have two functions.
>>> abc = lambda x: x**2
>>> xyz = lambda x: x**3
>>> type(abc), type(xyz)
(<class 'function'>, <class 'function'>)
# You have a key-value map of the names of the functions.
>>> d = {'1':'abc', '2':'xyz'}
>>> type(d['1'])
<class 'str'>
# Convert them into a key-value map of the index and the actual function.
>>> func_d = {k:eval(v) for k,v in d.items()}
>>> func_d[1]
See also:
You can getattr()
functions from their respective package by name, and then call them:
>>> import operator
>>> getattr(operator, "add")
<built-in function add>
>>> getattr(operator, "add")(1, 3)
4
So depending where your functions (e.g. abc_123
) are stored, that may help you. But consider storing functions themselves in your dictionary, instead of their names…
I also would suggest you read up on Python functions in more detail.