I'm not sure if I interpreted your question correctly. Generally you can create a dictionary table as @Jameson mentioned. But to parse the 4e15 expression without delimiters that separate between the characters would make your function limited to your set of choices provided in your code and technically not practical for larger scales, but works for smaller (limited) ones. So let me show general ways to achieve this goal:
def get_vals(vals, table):
res = []
for x in vals:
res.append(table.get(x, 'error')) # Or anything instead of error for invalid inputs
return res
>>> table = {'a':'test1', 'b':'test2'}
>>> get_vals('ab', table)
['test1', 'test2', 'error', 'error']
This function parses only one single character per a string input, if you enter string "15"
it'll try to fetch the value of "1"
from the dictionary and then "5"
, not string "15"
as a whole!
With the delimiter way, this will be more practical, easier to maintain and work with. For example:
def get_vals2(vals, table, delimiter = ','): # Default: split string with comma or whatever
res = []
for x in vals.split(delimiter):
res.append(table.get(x, 'error'))
return res
>>> get_vals2('1,a,15,b', table)
['error', 'test1', 'error', 'test2']
As you can see, if you input 1 and 5, it's "15"
when parsed, unlike the prior version. Another option would be to list all the combinations that the user may input, but what a tedious work! For instance:
table = {'4e15': your values, 'e415': your values, '415e': your values, continuing with others as well...}
Very complex and you'll write a huge list of combinations and not efficient when it comes to maintenance, because you hard-coded many combinations, a small little change could result in re-writing all the combinations again!