【问题标题】:filtering in a dictionary based on the index in python [closed]根据python中的索引在字典中过滤[关闭]
【发布时间】:2016-10-18 14:44:16
【问题描述】:

我有一个多字典如下:

 arcs, capacity = multidict({
                            ('Detroit', 'Boston'):   100,
                            ('Detroit', 'New York'):  80,
                            ('Detroit', 'Seattle'):  120,
                            ('Denver',  'Boston'):   120,
                            ('Denver',  'New York'): 120,
                            ('Denver',  'Seattle'):  120 })

您能告诉我如何选择索引中包含“Denver”的行吗?

【问题讨论】:

  • 我认为您有足够的代表知道如何用minimal reproducible example 提出正确的问题。请修改并相应地编辑您的问题。
  • multidict 来自哪里?
  • @Patrick Haugh:你能告诉我你的意思吗?对不起,我是python的初学者,不知道可能的东西。
  • multidict 不是 Python 的标准部分。这个从哪里来?在不知道它的结构的情况下,我们无法提供很多具体的建议。
  • @Patrick Haugh:其实我在python和优化软件界面的例子中看到过。我只是在这里复制它,因为它非常像我需要的。

标签: python dictionary filter


【解决方案1】:

我不确定multidictdict 有何不同,但您可以使用类似:

{k:v for k, v in d if 'Denver' in k}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-14
    • 2010-11-15
    • 2019-04-28
    • 1970-01-01
    • 2015-05-17
    • 1970-01-01
    • 2020-04-18
    • 2021-12-18
    相关资源
    最近更新 更多