【问题标题】:How can I transform a dictionnary into a list in python [duplicate]python - 如何将字典转换为python中的列表[重复]
【发布时间】:2021-04-25 18:10:08
【问题描述】:

我有一个作业,我有一个成绩字典,我需要取所有的值来找到这些成绩的平均值。但我不知道如何只取值{"DM1" : 90} 我只需要 90。有人可以帮忙吗?谢谢

bulletin = {"DS1": 15, "DM1": 13, "DS3": 14, "DM2": 15, "TP": 18, "DM3": 16, "DS3": 11} 

进入

grades = [] 

【问题讨论】:

    标签: python list dictionary


    【解决方案1】:

    grades = list(bulletin.values()) 应该做的工作。

    【讨论】:

    • 我试过了,但我的列表看起来像这样liste = [dict_values([1, 2]), dict_values([1, 2])]
    • 检查公告形状,可能你做错了什么。
    • 是的,我在你提供的公告字典上应用了这个并且它有效。
    【解决方案2】:

    这里是代码;它使用键和值创建字典,然后创建列表,分配字典的列表绑定,然后找到这些键的值,最后输出结果。

    # Create the dictionary
    bulletin = {"DS1": 15, "DM1": 13, "DS3": 14, "DM2": 15, "TP": 18, "DM3": 16, "DS3": 11}
    
    # Create a list and get the key values from the dictionary
    grades = list(bulletin.values())
    
    # Output the result
    print(grades)
    

    像魅力一样工作:

    【讨论】:

      【解决方案3】:

      您正在搜索的内置方法名为.values(),请查看 python 文档:Python - Built in Types

      【讨论】:

        猜你喜欢
        • 2019-11-18
        • 2016-01-10
        • 2013-11-12
        • 2017-07-31
        • 2012-07-12
        • 2019-09-28
        • 2018-06-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多