[Tutor] Iterate Through a Dictionary where the Values are Tuples
Dennis Lee Bieber
wlfraed at ix.netcom.com
Thu Jan 27 19:49:17 EST 2022
On Thu, 27 Jan 2022 15:08:28 -0800, Christopher Gass
<chris.gass.177 at gmail.com> declaimed the following:
>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']
>}
Is it guaranteed that the items in the lists are singular? (That is,
"BA0002" will only appear once is the entire structure?)
>
>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)
Since you didn't provide get_radio() I'm having to infer that it is
doing a reverse lookup -- that is, it is looking for "grid" in the list of
values, and then returning the KEY for "position".
> if radio_position == 'TAC_1':
> radio_position = TAC_1
If you have to expand this all keys in "position" it is going to get
confusing... Ignoring that get_radio() is returning a string which is bound
to radio_position... and then you replace that binding with another
dictionary.
> response_plan = radio_position[call_type]
> rec_station_order = station_order[grid]
> for unit_type in response_plan:
> for x in unit_type:
You don't seem to be doing anything with "x"
> for station in rec_station_order:
> for unit, details in units.items():
> for u_type, unit_station in details:
Details, at this point, is (example)
("Aid Unit", "STA 2")
so iterating returns "Aid Unit" as the first item which you are trying to
unpack into two values -- as a string, it unpacks into characters.
Try tuple unpacking
u_type, unit_station = details
> if u_type == unit_type and unit_station == station
>and unit not in result:
Seems rather confusing... The top loop is pulling unit_type, the second
loop does something with it, and down here you are comparing back to the
top unit...
You don't really seem to be doing much with all those dictionaries
> result.append(unit)
> apparatus.append(response_plan[i])
> i += 1
> return result
Why are you accumulating "apparatus" but not returning it?
>
>print(recommendations('bls','BA0001'))
Does this get closer to what you intended? Note the inversion of some
of your dictionaries.
-=-=-
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"]
]
}
TAC_7 = { "TBD" : ["TBD"] }
position = { "BA0001" : TAC_1,
"BA0002" : TAC_1,
"BA0003" : TAC_1,
"BA0004" : TAC_1,
"DF009" : TAC_7,
"DF010" : TAC_7,
"DF011" : TAC_7,
"DF013" : TAC_7,
"DF055" : TAC_7
}
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 = { ("Aid Unit", "STA 2") : "A2",
("Engine", "STA 2") : "E2"
}
def recommendation(call_type, grid):
call_type = call_type.strip().upper()
grid = grid.strip().upper()
result = []
apparatus = []
# ensure items exist
if (position.get(grid, None) is None or
station_order.get(grid, None) is None):
return "GRID %s is not valid" % grid
if position[grid].get(call_type, None) is None:
return "GRID/CALL_TYPE %s/%s is not valid" % (grid, call_type)
# response plan loop
for response_units in position[grid][call_type]:
for ru in response_units:
# station loop
for station in station_order[grid]:
unit = units.get( (ru, station), None)
if unit is not None:
result.append(unit)
apparatus.append(ru)
return (result, apparatus)
print(recommendation("bls", "ba0001") )
-=-=-
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed at ix.netcom.com http://wlfraed.microdiversity.freeddns.org/
More information about the Tutor
mailing list