简体   繁体   中英

Store Different Types of Values in One Key - Dictionary

I want to make a "travel" summary that looks like:

There are 4 starting points to reach Chicago . To access this city, you can use: Highway X, Highway Z, Highway ZZ.

The data that I currently have looks something like:

Highway X, NY, Chicago

Highway Z, LA, Chicago

Highway X, Austin, Chicago

Highway ZZ, Miami, Chicago

My question: What should I use to store these data properly?

I tried to use a dictionary with a list inside of it. However, it didn't work because I couldn't store the list of Highway. I only managed to get something like

{Chicago: [NY, LA, Austin]}

UPDATE!!

I just figured out that there's duplication in my data, and that's not a good news because I want the starting points to be distinct. Right now it looks like this:

Highway X, NY, Chicago

Highway X, NY, Chicago

Highway Z, LA, Chicago

Highway X, Austin, Chicago

Highway ZZ, Miami, Chicago

Highway X, NY, Chicago

The best way of organising your data depends on what you want to do with them.

Do you want to answer questions like "I'm in Austin, which Highway do I have to take to get to Chicago?"

Then your keys should be the departure cities:

lookup = {'Austin': 'Highway X',
          'NY': 'Highway X',
          'LA': 'Highway Z',
          'Miami': 'Highway ZZ'}

Because then you can answer the question by simply looking up the asker's starting point:

lookup['Austin']
# prints
# Highway X

But if you want to answer different questions like "Someone travelling on Highway X which city are they more likely to come from?" then you'd organise your data differently.

If you just want to print them, use a 'lil' (list of lists)

table = [["Highway X", "NY", "Chicago"],
         ["Highway Z", "LA", "Chicago"],
         ["Highway X", "Austin", "Chicago"],
         ["Highway ZZ", "Miami", "Chicago"]]

Then you can easily format

for row in table:
    print("{:12}{:8}{:10}".format(*row))

Use a list of tuples in your dictionary. The first item in each tuple would be the highway, the second the starting point.

routes = {'Chicago': [('Highway X', 'NY'), ('Highway Z', 'LA'), ('Highway AX', 'Austin'), ('Highway ZZ', 'Miami')]}

Then it's trivial to produce the summary:

for city in routes:
    paths = ', '.join(sorted({route[0] for route in routes[city]}))
    print("There are {} starting points to reach {}. To access this city, you can use: {}".format(len(routes[city]), city, paths))

The first line uses a set comprehension to remove duplicate highways and then sorts and joins them to make a string. That string as well as the destination city and number of routes is then used to construct the final summary string. The output of the above code would be:

There are 4 starting points to reach Chicago. To access this city, you can use: Highway X, Highway Z, Highway ZZ

An easy way to construct the routes dict is to use a collections.defaultdict of lists. Assuming that your data is coming from a CSV file:

import csv
from collections import defaultdict

routes = defaultdict(list)

with open('data.csv') as f:
    reader = csv.reader(f)
    for highway, start, dest in reader:
        routes[dest].append((highway, start))

Nest some dictionaries. This allows you to follow a "route."

{'Highway X': {'Austin': 'Chicago', 'NY': 'Chicago'},
 'Highway Z': {'LA': 'Chicago'},
 'Highway ZZ': {'Miami': 'Chicago'}}

Alternatively:

{'Austin': {'Highway X': 'Chicago'},
 'LA': {'Highway Z': 'Chicago'},
 'Miami': {'Highway ZZ': 'Chicago'},
 'NY': {'Highway X': 'Chicago'}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM