【问题标题】:Python: how to print individual dictionary values?Python:如何打印单个字典值?
【发布时间】:2015-12-23 05:19:27
【问题描述】:

我有以下代码:

voorzieningen = {
    "NS-service- en verkooppunt": {"type:": "verkoop- en reisinformatie", "locatie": "spoor 11/12"},
    "Starbucks": {"type": "Winkels en restaurants", "locatie": "spoor 18/19"},
    "Smeulers": {"type": "Winkels en restaurants", "locatie": "spoor 5/6"},
    "Kaartautomaat": {"type": "Verkoop- en reisinformatie", "locatie": "spoor 1"},
    "Toilet": {"type": "Extra voorziening", "locatie": "spoor 4"}
    }


def voorzieningen_op_utrecht():
    for voorziening in voorzieningen:
        print(voorziening)


voorzieningen_op_utrecht()

我想要得到的是以下内容:

<First value> "is of the type " <second value> "and is located at" <third value>

例如:

星巴克属于 Winkels en 餐厅类型,位于 spoor 18/19。

我希望它是一个 for 循环,以便打印所有值。

附:为荷兰人道歉,但这不应该让代码更难理解。

【问题讨论】:

  • "First", "second""third" 没有意义,字典不保证顺序.你看过str.format吗?另外,请注意,您的其中一个键是 'type:' 而不是 'type'

标签: python for-loop dictionary


【解决方案1】:

你可以做类似的事情

for key, value in voorzieningen.items():
    print('{} is of the type {} and is located at {}'.format(key, value['type'], value['locatie']))

示例的输出

NS-service- en verkooppunt 属于 verkoop- en reisinformatie 类型,位于 spoor 11/12
Kaartautomaat 属于 Verkoop- en reisinformatie 类型,位于 spoor 1
Smeulers 属于 Winkels en 餐厅类型,位于 spoor 5/6
星巴克属于 Winkels en 餐厅类型,位于 spoor 18/19
厕所属于 Extra voorziening 类型,位于 spoor 4

【讨论】:

    【解决方案2】:
    for key in voorzieningen:
      print("%s is of type %s and is located at %s" % (key, voorzieningen[key]['type'], voorzieningen[key]['location']))
    

    【讨论】:

    • C 风格的% 字符串格式在这一点上有点过时了!
    【解决方案3】:
    for k, v in voorzieningen.items():
        print('{} is of the type {} and is located at {}'.format(k, v['type'], v['locatie']))
    

    【讨论】:

    • 将键移到格式字符串中可能更简洁 - '{0} is of the type {1[type]} and is located at {1[locatie]}'。随着字符串变长,这样可以更轻松地确保正确的东西在正确的位置。
    【解决方案4】:

    我愿意:

    template = '{place} is of the type {data[type]} and is located at {data[locatie]}'
    for place, data in voorzieningen.items():
        print(template.format(place=place, data=data))
    

    这会在格式字符串中保留大量信息,从而更容易验证您是否在做正确的事情。但是,我得到了输出:

    Starbucks is of the type Winkels en restaurants and is located at spoor 18/19
    Smeulers is of the type Winkels en restaurants and is located at spoor 5/6
    Traceback (most recent call last):
      File "<pyshell#9>", line 2, in <module>
        print(template.format(place=place, data=data))
    KeyError: 'type'
    

    因为其中一个键有 'type:' 而不是 'type';小心未经处理的输入数据!


    奖金事实

    从 Python 3.6 开始,您将能够使用 literal string interpolation 更简洁地执行此操作,例如:

    for place, data in voorzieningen.items():
        print(f'{place} is of the type {data[type]} and is located at {data[locatie]}')
    

    【讨论】:

    • 夏普的名字和行动 :)
    • 完美!非常感谢。
    猜你喜欢
    • 2018-08-06
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 2019-04-18
    • 2011-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多