I have the following dictionaries:
input_processed = {'units' : {'g' : 'g',
                              'dx' : 'cm',
                              'po' : 'bar',
                              'muo' : 'mPas'},
                   'g' : 1.0,
                   'dx' : [10.0, 20.0, 10.0],
                   'muo' : {'po' : [0.0, 1.0],
                            'muo' : [32.7, 32.7]},
                   'phi' : 0.05}
commands_variables = {'units' : [],
                      'g' : ['g'],
                      'dx' : ['dx'],
                      'muo' : ['po', 'muo'],
                      'phi' : ['phi']}
conversion_factors = {'g' : 9.81,
                      'cm' : 0.01,
                      'bar' : 10**5,
                      'mPas' : 10**-3}
My aim is to multiply each float in input_processed by a factor defined in conversion_factors for some variables. Since a command (highest level keys in input_processed can define multiple variables, these are mapped in commands_variables.
So first I need to check, if the variable is contained in input_processed['units'], since phi has no unit. Then for each variable in every command I need to get the unit. In the next step I get the conversion factor depending on the unit from conversion_factors. Since the values of the highest level keys in input_processed can be of the type float, list or dict, I wrote a different method for each case. This is put into a function and called in a loop over input_processed.
def convert_unit(data, command, parameter):
    for it in commands_variables[command]:
        if it in data['units']:
            variable = it
        else:
            continue
        unit = data['units'][variable]
        conversion_factor = conversion_factors[unit]
        if type(parameter) is float:
            converted = parameter*conversion_factor
        elif type(parameter) is list:
            converted = [parameter[it]*conversion_factor
                         for it in range(len(parameter))]
        elif type(parameter) is OrderedDict:
            for key, value in parameter.items():
                value = parameter[key]
                converted = [value[it]*conversion_factor
                             for it in range(len(value))]
        return converted
input_converted = {}
for key, value in input_processed.items():
    input_converted[key] = convert_unit(input_processed, key, value)
The desired output is for the function
9.81
[0.1, 0.2, 0.1]
{'po' : [0.0, 100000.0], 'muo' : [0.0327, 0.0327]}
0.05
and for the main program
input_processed = {'units' : {'g' : 'g',
                              'dx' : 'cm',
                              'po' : 'bar',
                              'muo' : 'mPas'},
                   'g' : 9.81,
                   'dx' : [0.1, 0.2, 0.1],
                   'muo' : {'po' : [0.0, 100000.0],
                            'muo' : [0.0327, 0.0327]},
                   'phi' : 0.05}
I managed to convert floats and lists but no dictionaries. In this shortened problem statement, I had the error that the local variable 'converted' is referenced before the assignment.
If somebody has an easier way to convert the quantities, I would appreciate that.
 
     
    