【问题标题】:How to iterate over and access individual values in a dictionary如何迭代和访问字典中的单个值
【发布时间】:2019-03-12 08:47:29
【问题描述】:

我正在做一些 python 作业,我被要求定义一个函数,该函数接受一个字典,该字典将公交路线映射到公交站,并返回一个字典,将公交站映射到停靠在该站的公交路线列表。输入将是这样的:

{"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"]}

我需要以某种方式将值变成键,同时检查这些值是否也是任何其他键中的值。基本上,我很难弄清楚如何访问这些值并使它们成为新键(不重复),而无需实际对它们进行硬编码。

【问题讨论】:

标签: python


【解决方案1】:

如果您使用DictionaryList Comprehension,这实际上非常简单。您可以在一行中获得所需的输出。

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)

更易读但更深入的理解

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

输出:

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

【讨论】:

  • 我最好不要使用带有键和值功能的项目和 go9ng。我打算用 Dict 理解它,但很难开始使用 .items() v. 很好的答案
  • 您是否也以某种方式对字典进行排序?我的输出是相同的,但是重复的站点混合在非重复站点中
【解决方案2】:

因此,如果我正确理解了这个问题,您希望找到沿途的停靠点,即同一辆公共汽车经过的停靠点,本质上是寻找公共汽车路线重复。

请看下面的代码。

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)

我们通过使用 bus_routes.items() 进行迭代得到一个变量 (y),其中 x 是路线名称,y 是停靠点名称列表。然后我们使用y 创建另一个迭代,并尝试查看route_dup 中是否已经存在具有该停止名称的键,如果它不存在,它会捕获KeyError 并使用名称的值创建它列表中的路由,但是,如果键确实存在,我们可以安全地说它将是我们已经创建的列表,因此append() 给它,并带有下一个路由名称。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-10
    • 2023-02-02
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多