【问题标题】:Sorting a lists within a dictionary using list data使用列表数据对字典中的列表进行排序
【发布时间】:2019-04-29 11:46:07
【问题描述】:

我的问题是我试图在字典中对这些列表进行排序,我使用字典名称作为字段名称,将列表用作数据行。我感觉我使用了错误的方法(应该是元组还是数据框还是什么?)

我尝试过使用sorted(),但无法通过仅按键排序(例如namescore1score2)。我想保持键顺序,但重新排列值,同时保持键之间的关系。

这是我想按score1(或score2)排序的示例数据集:

scores = {'name': ['joe', 'pete', 'betsy', 'susan', 'pat'], 'score1': [99, 90, 84, 65, 100], 'score2': [85, 91, 90, 55, 98]}

score1排序后,我想看看:

pat, joe, pete, betsy, susan

score1排序后,我想看看:

pat, pete, betsy, joe, susan

【问题讨论】:

    标签: python-3.x list sorting dictionary


    【解决方案1】:

    如果您自己创建scores,如果它的结构不同,即[{'name': 'joe', 'score1': 1, 'score2': 2}, ...],会容易得多。

    那么sorted的用法就很简单了:

    scores = [{'name': 'joe', 'score1': 99, 'score2': 85},
              {'name': 'pete', 'score1': 90, 'score2': 91},
              {'name': 'betsy', 'score1': 84, 'score2': 90},
              {'name': 'susan', 'score1': 65, 'score2': 55},
              {'name': 'pat', 'score1': 100, 'score2': 98}]
    
    by_score_1 = [d['name'] for d in sorted(scores, key=lambda d: d['score1'], reverse=True)]
    print(by_score_1)
    by_score_2 = [d['name'] for d in sorted(scores, key=lambda d: d['score2'], reverse=True)]
    print(by_score_2)
    

    输出:

    ['pat', 'joe', 'pete', 'betsy', 'susan']
    ['pat', 'pete', 'betsy', 'joe', 'susan']
    

    【讨论】:

    • ...这比我建议的更高效、更清晰!
    • 我可以重组分数,所以这看起来很有趣。然后我需要在创建新测试时添加新密钥。这似乎很容易通过:for item in scores: item.update( {'score3': 90}) 完成,但是如果我需要由学生单独更新怎么办?如何将“Pat”更新为 75 分中的 3 分?
    • @DouglasWood 这是一个非常有效的论点,我建议的数据结构效率有点低。您需要遍历整个列表,直到找到正确的字典:for person_data in scores: if person_data['name'] == 'pat': person_data['score1'] = new_score
    【解决方案2】:

    另一个答案很好。您还可以将其转换为元组列表,然后轻松对其进行排序:

    scores = {
        'name': ['joe', 'pete', 'betsy', 'susan', 'pat'], 
        'score1': [99, 90, 84, 65, 100], 
        'score2': [85, 91, 90, 55, 98]}
    
    t = list(zip(*scores.values()))
    print(t)
    

    输出:

    [('joe', 99, 85), ('pete', 90, 91), ('betsy', 84, 90), ('susan', 65, 55), ('pat', 100, 98)]
    

    然后就可以排序了:

    # Sort by score1
    print(sorted(t, key=lambda x: (x[1]), reverse=True))
    
    # Sort by score2
    print(sorted(t, key=lambda x: (x[2]), reverse=True))
    
    # Sort by both scores:
    print(sorted(t, key=lambda x: (x[1], x[2]), reverse=True))
    

    只是解决同一问题的不同方法。这样做,您也可以轻松打印个人的分数。

    【讨论】:

      【解决方案3】:

      这是可能的:

      print(sorted(scores['name'], reverse=True, 
                   key=lambda x: scores['score1'][scores['name'].index(x)]))
      # ['pat', 'joe', 'pete', 'betsy', 'susan']
      print(sorted(scores['name'], reverse=True, 
                   key=lambda x: scores['score2'][scores['name'].index(x)]))
      # ['pat', 'pete', 'betsy', 'joe', 'susan']
      

      【讨论】:

      • 如果我想显示姓名和分数怎么办?另外,如果我不仅要打印而且要更新字典以按 score1 排序怎么办?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-14
      • 1970-01-01
      • 2021-07-12
      相关资源
      最近更新 更多