[Tutor] Iterate Through a Dictionary where the Values are Tuples

Christopher Gass chris.gass.177 at gmail.com
Thu Jan 27 18:08:28 EST 2022


Hello,
I'm working on a project to write a script that will do some basic unit
recommendations for a fire department while their usual computer system is
offline. Right now it's a proof of concept, so it's not built out fully. As
part of this script, I'm attempting to iterate over a dictionary where the
values are tuples. More specifically, the keys are the unit numbers for the
fire apparatus and the values are a tuple containing the unit type (Engine,
Aid Car, etc.) and assigned station. I'm calling the tuple containing the
unit type and station assignment "details" and I'm trying to then iterate
through that tuple to see if the unit type and station assignment match
what's being requested in the response plan. When I try to
iterate through "details" however, I'm receiving a "too many values to
unpack" error. Do you have a solution to this error or maybe another way I
could approach the problem? Thanks for the help.

position = {
    'TAC_1':['BA0001', 'BA0002', 'BA0003', 'BA0004'],
    'TAC_7':['DF009', 'DF010', 'DF011', 'DF013', 'DF055']
}

TAC_1 = {
    'BLS':[['Aid Unit', 'Engine', 'Ladder']],
    'BLSN':[['Aid Unit', 'Engine', 'Ladder']],
    'MED':['Medic Unit', 'Engine'],
    'MEDX':['Medic Unit', ['Engine', 'Ladder'], ['Engine', 'Ladder'], 'Medical
Services Officer']
}

station_order = {
    'BA0001':['STA 2', 'STA 3', 'STA 1', 'STA 5', 'STA 4', 'STA 6', 'STA 7',
'STA 61', 'STA 63', 'STA 82'],
    'BA0002':['STA 2', 'STA 3', 'STA 1', 'STA 5', 'STA 4', 'STA 6', 'STA 7',
'STA 61', 'STA 63', 'STA 82']
}

units = {
    'A2':('Aid Unit', 'STA 2'),
    'E2':('Engine', 'STA 2'),
}

def recommendations(call_type,grid):
    call_type = call_type.strip().upper()
    grid = grid.strip().upper()
    result = []
    apparatus = []
    i = 0
    unit_details = ()
    radio_position = get_radio(grid)
    if radio_position == 'TAC_1':
        radio_position = TAC_1
    response_plan = radio_position[call_type]
    rec_station_order = station_order[grid]
    for unit_type in response_plan:
        for x in unit_type:
            for station in rec_station_order:
                for unit, details in units.items():
                    for u_type, unit_station in details:
                        if u_type == unit_type and unit_station == station
and unit not in result:
                            result.append(unit)
                            apparatus.append(response_plan[i])
                            i += 1
    return result

print(recommendations('bls','BA0001'))
-Christopher Gass
 chris.gass.177 at gmail.com


More information about the Tutor mailing list