m0_52742114 2022-02-03 00:44 采纳率: 0%
浏览 31

#python#从字典中获取字典并根据条件生成新字典

假如有成绩表如下:

score = {"A": {"chinese": 100,"math": 90, "english": 80},
         "B": {"chinese": 31, "math": 71, "english": 91},
         "C": {"chinese": 88, "math": 46, "english": 100},
         "D": {"chinese": 60, "math": 99, "english": 71},
         "E": {"chinese": 56, "math": 80, "english": 61},
         "F": {"chinese": 45, "math": 57, "english": 45}}

请编写代码,分别统计出每个科目,不及格(分数小于60)人员的名字;期望最终输出结果如下:

{'chinese':["B","E","F"],"math": ["C","F"],"english":["F"]}
  • 写回答

2条回答 默认 最新

  • 关注

    你题目的解答代码如下:

    score = {"A": {"chinese": 100,"math": 90, "english": 80},
             "B": {"chinese": 31, "math": 71, "english": 91},
             "C": {"chinese": 88, "math": 46, "english": 100},
             "D": {"chinese": 60, "math": 99, "english": 71},
             "E": {"chinese": 56, "math": 80, "english": 61},
             "F": {"chinese": 45, "math": 57, "english": 45}}
    
    dic = {'chinese':[],"math": [],"english":[]}
    for m,c in score.items():
        for k,v in c.items():
            if v<60:
                dic[k].append(m)
    print(dic)
    

    img

    如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

    img

    评论

报告相同问题?

问题事件

  • 创建了问题 2月3日

悬赏问题

  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格
  • ¥15 kali linux用wget archive.kali.org/archive-key.asc指令下载签名无效(失败)