【问题标题】:Python sort dictionary based on value inside a list [duplicate]Python根据列表中的值对字典进行排序[重复]
【发布时间】:2022-01-21 09:17:20
【问题描述】:

我尝试对以下包含列表作为值的字典进行排序

假设我有这本词典


x={"George": [68, 0% to work, 70%], "Nikolas": [25, 29% to work, 94%], "Beat": [18, 100% to work, 10%] }


我想根据列表中的第一个值对这个字典进行排序

我想要以下列表

"Beat": [18, 100% to work, 10%], "Nikolas": [25, 29% to work, 94%], "George": [68, 0% to work, 70%]

【问题讨论】:

    标签: python dictionary


    【解决方案1】:
    >>> dict(sorted(x.items(), key=lambda item: item[1][0]))
    
    out:
    
    {'Beat': [18, '100% to work', '10%'], 'Nikolas': [25, '29% to work', '94%'], 'George': [68, '0% to work', '70%']}
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多