Home > other >  Python Dictionary Key Error when Adding to Nested Dictionary
Python Dictionary Key Error when Adding to Nested Dictionary

Time:07-18

I'm trying to make a sort-of-chart of a subway map into a more useful graph which can be fed into my Dijkstra Algorithm code.

My goal is to get something like this [['station a', 'station b', '1', 'V7'], ['station a', 'station c', '1', 'N8']] into {"station a" : {"station b" : "V71", "station c" : "N81"}, "station b" : {"station a" : "V71"}, "station c" : {"station a" : "N81"}}. Also, as shown in the Station A Key, I'd like it to be possible for a station to be linked to more than one station if necessary. But, I keep on getting an error:

  File "main.py", line 11, in <module>
    newdata[station][section[0]] = str(section[3])   str(section[2]);
KeyError: 'koschee'
python3 exited with code 1...

My compiler is a little weird with syntax errors, so if this looks a little fake, I didn't mean it that way. Here's my code, anyway.

data = [
    ['ends cross', 'flora junction', '86', 'R1', '1'], ['flora junction', 'weavingstone', '45', 'R1', '2'], ['furiex', 'weavingstone', '45', 'R1', '3'], ['furiex', 'stat rd 1', '27', 'R1', '4'], ['stat rd 1', 'stat rd 2', '25', 'R1', '5'], ['stat rd 2', 'branchia', '20', 'R1', '6'], ['branchia', 'ends way', '31', 'R1', '7'], ['clocksroad', 'globesworth', '90', 'R2', '1'], ['big sittee statin', 'globesworth', '30', 'R2', '2'], ['big sittee statin', 'weavingstone', '18', 'R2', '3'], ['furiex', 'weavingstone', '45', 'R2', '4'], ['furiex', 'stat rd 1', '27', 'R2', '5'], ['stat rd 1', 'stat rd 2', '25', 'R2', '6'], ['stat rd 2', 'branchia', '20', 'R2', '7'], ['branchia', 'ends way', '31', 'R2', '8'], ['big sittee statin', 'biggins hill', '15', 'R3', '1'], ['big sittee statin', 'weavingstone', '18', 'R3', '2'], ['furiex', 'weavingstone', '45', 'R3', '3'], ['furiex', 'stat rd 1', '27', 'R3', '4'], ['stat rd 2', 'stat rd 1', '25', 'R3', '5'], ['stat rd 2', 'branchia', '20', 'R3', '6'], ['branchia', 'ends way', '31', 'R3', '7'], ['ends cross', 'flora junction', '86', 'O1', '1'], ['23d station', 'flora junction', '27', 'O1', '2'], ['barbi gorl', '23d station', '32', 'O1', '3'], ['barbi gorl', 'stat rd 2', '31', 'O1', '4'], ['ends way', 'stat rd 2', '70', 'O1', '5'], ['baltstaten', '23d station', '387', 'O2', '1'], ['barbi gorl', '23d station', '32', 'O2', '2'], ['barbi gorl', 'stat rd 2', '31', 'O2', '3'], ['ends way', 'stat rd 2', '70', 'O2', '4'], ['ends cross', 'flora junction', '86', 'Y', '1'], ['baltstaten', 'flora junction', '410', 'Y', '2'], ['baltstaten', 'easton', '368', 'Y', '3'], ['barbi gorl', 'easton', '43', 'Y', '3'], ['bolandok', 'stat rd 1', '100', 'Y', '4'], ['biggins hill', 'bolandok', '17', 'Y', '5'], ['clocksroad', 'globesworth', '90', 'G1', '1'], ['big sittee statin', 'weavingstone', '18', 'G1', '2'], ['furiex', 'weavingstone', '45', 'G1', '3'], ['ends cross', 'flora junction', '86', 'G2', '1'], ['flora junction', 'weavingstone', '45', 'G2', '2'], ['furiex', 'weavingstone', '45', 'G1', '3'], ['branchia', 'ends way', '31', 'T1', '1'], ['branchia', 'easton', '31', 'T1', '2'], ['branchia', 'ends way', '31', 'T2', '1'], ['branchia', 'centralia pa', '22', 'T2', '2'], ['branchia', 'ends way', '31', 'T3', '1'], ['branchia', 'sam westing', '31', 'T3', '2'], ['centralia pa', 'easton', '31', 'T4', '1'], ['centralia pa', 'sam westing', '31', 'T4', '2'], ['baltstaten', '23d station', '387', 'B1', '1'], ['23d station', 'furiex', '46', 'B1', '2'], ['biggins hill', 'furiex', '30', 'B1', '3'], ['biggins hill', 'bolandok', '17', 'B1', '4'], ['bolandok', 'koschee', '86', 'B1', '5'], ['ends way', 'koschee', '28', 'B1', '6'], ['baltstaten', '23d station', '387', 'B2', '1'], ['23d station', 'furiex', '46', 'B2', '2'], ['biggins hill', 'furiex', '30', 'B2', '3'], ['big sittee statin', 'biggins hill', '15', 'B2', '4'], ['big sittee statin', 'globesworth', '30', 'B2', '5'], ['biggins hill', 'bolandok', '17', 'B3', '1'], ['bolandok', 'koschee', '86', 'B3', '2'], ['ends way', 'koschee', '28', 'B3', '3'], ['big sittee statin', 'biggins hill', '15', 'B3', '4'], ['big sittee statin', 'globesworth', '30', 'B3', '5'], ['biggins hill', 'ends way', '87', 'B4', '1'], ['big sittee statin', 'biggins hill', '15', 'B4', '2'], ['big sittee statin', 'globesworth', '30', 'B4', '3'], ['clocksroad', 'globesworth', '90', 'V', '1'], ['globesworth', 'the new yorham', '0', 'V', '2'], ['ends cross', 'the new yorham', '35', 'V', '3'], ['clocksroad', 'globesworth', '90', 'P', '1'], ['big sittee statin', 'globesworth', '30', 'P', '2'], ['big sittee statin', 'carpetia', '23', 'P', '3'], ['carpetia', 'weavingstone', '41', 'P', '4'], ['furiex', 'weavingstone', '45', 'P', '5'], ['flora junction', 'weavingstone', '45', 'P', '6'], ['ends cross', 'flora junction', '86', 'P', '7'], ['baltstaten', 'aquarius', '30', 'Q1', '1']];
stations = ['koschee', 'bolandok', 'sam westing', 'centralia pa', 'easton', 'branchia', 'ends way', 'stat rd 2', 'stat rd 1', 'barbi gorl', 'baltstaten', '23d station', 'ends cross', 'flora junction', 'furiex', 'weavingstone', 'biggins hill', 'clocksroad', 'carpetia', 'globesworth', 'big sittee statin', 'the new yorham'];

newdata = {};
for station in stations:
  for section in data:
    if section[0] == station:
      newdata[station][section[1]] = str(section[3])   str(section[2]);
    elif section[1] == station:
      newdata[station][section[0]] = str(section[3])   str(section[2]);

What should I do? Thanks! (This is a map of the subway I am trying to graph below. This is a map of the subway I am trying to graph.

CodePudding user response:

You don't actually create the nested dicts that you want.

You should create them like this: newdata[station] = {<key>:<str>}

Then the code looks like this:

newdata = {}
for station in stations:
    for section in data:
        if section[0] == station:
            newdata.setdefault(station,{}).update({section[1]: str(section[3])   str(section[2])})
    elif section[1] == station:
      newdata.setdefault(station,{}).update({section[0]: str(section[3])   str(section[2])})

(btw you don't need ; at the end of lines in python)

Update: Changed the dict entries to add more entries as they are found.

Sample output:

{'koschee': {'bolandok': 'B386', 'ends way': 'B328'}, 'bolandok': {'stat rd 1': 'Y100', 'biggins hill': 'B317', 'koschee': 'B386'}, ...
  • Related