【问题标题】:How to print values in a dictionary that fall below a certain percentage? [closed]如何在字典中打印低于某个百分比的值? [关闭]
【发布时间】:2020-04-02 01:33:43
【问题描述】:

我需要一个代码来打印出任何一个或多个测验分数低于 70 分的学生的姓名,然后是该学生测验分数低于 70 分的排序列表。

即。

quiz_scores = {
    'Corbin': [92, 66, 88, 91, 100, 95, 94],
    'Kevin': [100, 99, 100, 98, 95, 98, 99],
    'Hannah': [79, 67, 78, 81, 70, 55, 74],
    'Ben': [94, 88, 85, 100, 91, 79, 88],
    'Yasaswini': [99, 90, 88, 91, 89, 95, 94],
    'Veda': [79, 97, 88, 82, 70, 95, 94],
    'Jack': [59, 67, 74, 91, 70, 75, 56]
}

【问题讨论】:

  • 你试过什么?您具体需要哪些帮助?

标签: python dictionary key


【解决方案1】:

有工作代码

quiz_scores = {
    'Corbin': [92, 66, 88, 91, 100, 95, 94],
    'Kevin': [100, 99, 100, 98, 95, 98, 99],
    'Hannah': [79, 67, 78, 81, 70, 55, 74],
    'Ben': [94, 88, 85, 100, 91, 79, 88],
    'Yasaswini': [99, 90, 88, 91, 89, 95, 94],
    'Veda': [79, 97, 88, 82, 70, 95, 94],
    'Jack': [59, 67, 74, 91, 70, 75, 56]
}

print({name: [score for score in scores if score < 70] for name, scores in quiz_scores.items() if min(*scores) < 70})

和样本输出

> python run.py
{'Corbin': [66], 'Hannah': [67, 55], 'Jack': [59, 67, 56]}

【讨论】:

    【解决方案2】:

    如果你可以使用熊猫:

    import pandas as pd
    df= pd.DataFrame(quiz_scores).T.unstack()
    a = pd.DataFrame(df[df < 70])
    a = a[0].sort_values() 
    a.reset_index().groupby('level_1')[0].apply(list).to_dict()                                                                                                                                       
    # {'Corbin': [66], 'Hannah': [55, 67], 'Jack': [56, 59, 67]}
    

    【讨论】:

      猜你喜欢
      • 2011-07-15
      • 1970-01-01
      • 2020-06-19
      • 2023-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-18
      • 2019-11-09
      相关资源
      最近更新 更多