简体   繁体   English

将不同类型的值存储在一个键中-字典

[英]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 . 到达芝加哥4个起点。 To access this city, you can use: Highway X, Highway Z, Highway ZZ. 要访问该城市,您可以使用: X 高速公路,Z 高速公路,ZZ高速公路。

The data that I currently have looks something like: 我目前拥有的数据如下所示:

Highway X, NY, Chicago 纽约州芝加哥X高速公路

Highway Z, LA, Chicago 芝加哥Z高速公路,芝加哥

Highway X, Austin, Chicago X高速公路,奥斯汀,芝加哥

Highway ZZ, Miami, Chicago ZZ高速公路,迈阿密,芝加哥

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!! 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 纽约州芝加哥X高速公路

Highway X, NY, Chicago 纽约州芝加哥X高速公路

Highway Z, LA, Chicago 芝加哥Z高速公路,芝加哥

Highway X, Austin, Chicago X高速公路,奥斯汀,芝加哥

Highway ZZ, Miami, Chicago ZZ高速公路,迈阿密,芝加哥

Highway X, NY, Chicago 纽约州芝加哥X高速公路

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?" 但是,如果您想回答其他问题,例如“有人在X高速公路上旅行,他们更有可能来自哪个城市?” then you'd organise your data differently. 那么您将以不同的方式组织数据。

If you just want to print them, use a 'lil' (list of lists) 如果只想打印它们,请使用“ lil”(列表列表)

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. 第一行使用set comprehension删除重复的高速公路,然后对其进行排序和连接以创建字符串。 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. 构造routes字典的一种简单方法是使用列表的collections.defaultdict Assuming that your data is coming from a CSV file: 假设您的数据来自CSV文件:

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'}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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