1

I'm working on some python homework and I am asked to define a function that takes in a dictionary that maps bus routes to bus stops and returns a dictionary mapping bus stops to list of bus routes which stop at that stop. The input would be something like this:

{"Lentil": ["Chinook", "Orchard", "Valley", "Emerald","Providence",
"Stadium", "Main", "Arbor", "Sunnyside", "Fountain", "Crestview",
"Wheatland", "Walmart", "Bishop", "Derby", "Dilke"],
"Wheat": ["Chinook", "Orchard", "Valley", "Maple","Aspen", "TerreView",
"Clay", "Dismores", "Martin", "Bishop", "Walmart", "PorchLight",
"Campus"]}

I need to somehow make the values into keys and at the same time check if those values are values in any other key as well. Basically, I'm having a hard time trying to figure out how I can access the values and make them the new keys (no duplicates) without actually hard coding them.

Larry.Fish
  • 123
  • 1
  • 8
  • What do you mean "make the values into keys"? An example might be helpful. – l'L'l Mar 11 '19 at 23:59
  • https://stackoverflow.com/questions/3294889/iterating-over-dictionaries-using-for-loops https://stackoverflow.com/questions/1602934/check-if-a-given-key-already-exists-in-a-dictionary and append to the new lists – B. Go Mar 12 '19 at 00:05
  • I mean I need to make the values of the dictionary into the new keys so for this it would be something like: {"Chinook": ["Lentil", "Wheat"], "Orchard":["lentil", "wheat"]...} – Larry.Fish Mar 12 '19 at 00:19
  • 1
    looks like a dupe of, related to, or refer to [Swap dictionary keys and values when values are lists](https://stackoverflow.com/questions/31674403/swap-dictionary-keys-and-values-when-values-are-lists) – chickity china chinese chicken Mar 12 '19 at 00:52

2 Answers2

2

This is actually very easy if you use Dictionary and List Comprehension. You can get the required output in one line.

d1={"Lentil": ["Chinook", "Orchard", "Valley", "Emerald","Providence",
"Stadium", "Main", "Arbor", "Sunnyside", "Fountain", "Crestview",
"Wheatland", "Walmart", "Bishop", "Derby", "Dilke"],
"Wheat": ["Chinook", "Orchard", "Valley", "Maple","Aspen", "TerreView",
"Clay", "Dismores", "Martin", "Bishop", "Walmart", "PorchLight",
"Campus"]}
d2={x:[y for y in d1.keys() if x in d1[y]] for l in d1.values() for x in l}
print(d2)

A more readable but longer comprehension

d2={stop:[route for route in d1.keys() if stop in d1[route]] for stop_list in d1.values() for stop in stop_list}

Output:

{'Chinook': ['Lentil', 'Wheat'], 'Orchard': ['Lentil', 'Wheat'], 'Valley': ['Lentil', 'Wheat'], 'Emerald': ['Lentil'], 'Providence': ['Lentil'], 'Stadium': ['Lentil'], 'Main': ['Lentil'], 'Arbor': ['Lentil'], 'Sunnyside': ['Lentil'], 'Fountain': ['Lentil'], 'Crestview': ['Lentil'], 'Wheatland': ['Lentil'], 'Walmart': ['Lentil', 'Wheat'], 'Bishop': ['Lentil', 'Wheat'], 'Derby': ['Lentil'], 'Dilke': ['Lentil'], 'Maple': ['Wheat'], 'Aspen': ['Wheat'], 'TerreView': ['Wheat'], 'Clay': ['Wheat'], 'Dismores': ['Wheat'], 'Martin': ['Wheat'], 'PorchLight': ['Wheat'], 'Campus': ['Wheat']}
Bitto
  • 7,937
  • 1
  • 16
  • 38
  • I would have been better off not using items and go9ng with keys and values functions. I was going to Dict comprehension it but struggled to get into work with .items() v. Nice answer – Swift Mar 12 '19 at 09:32
  • Do you also sort your dictionary somehow? Mine is the same output, but the duplicated stops are mixed among the non-duplicated stops – Swift Mar 12 '19 at 09:33
1

So if I've understood the question correctly, you want to find stops along the routes, where there are stops visited by the same bus, essentially finding bus route duplication.

Please see the below code.

bus_routes = {"Lentil": ["Chinook", "Orchard", "Valley", "Emerald","Providence", "Stadium", "Main", "Arbor", "Sunnyside", "Fountain", "Crestview", "Wheatland", "Walmart", "Bishop", "Derby", "Dilke"], "Wheat": ["Chinook", "Orchard", "Valley", "Maple","Aspen", "TerreView", "Clay", "Dismores", "Martin", "Bishop", "Walmart", "PorchLight", "Campus"]}
route_dup = {}
for x,y in bus_routes.items():
    for z in y:
        try:
            if route_dup[z]:
                route_dup[z].append(x)
        except KeyError:
            route_dup[z] = [x]

print(route_dup)

We get a variable (y) from iterating with bus_routes.items() where x is the route name and y is the stop name list. We then create another iteration with y and try to see if a key with that stop name already exists in route_dup and if it doesn't exist, it catches the KeyError and creates it with the value of the name of the route in list, however, if the key does exist, we can safely say it will the list we created already, and therefore append() to it, with the next route name.

Hope this helps.

Swift
  • 1,663
  • 1
  • 10
  • 21