【问题标题】:Sort a nested dictionary in Python在 Python 中对嵌套字典进行排序
【发布时间】:2016-10-31 21:43:03
【问题描述】:

我有以下字典。

var = a = { 
  'Black': { 'grams': 1906, 'price': 2.05},
  'Blue': { 'grams': 9526, 'price': 22.88},
  'Gold': { 'grams': 194, 'price': 8.24},
  'Magenta': { 'grams': 6035, 'price': 56.69},
  'Maroon': { 'grams': 922, 'price': 18.76},
  'Mint green': { 'grams': 9961, 'price': 63.89},
  'Orchid': { 'grams': 4970, 'price': 10.78},
  'Tan': { 'grams': 6738, 'price': 50.54},
  'Yellow': { 'grams': 6045, 'price': 54.19}
}

如何根据price 对其进行排序。所以生成的字典将如下所示。

result = { 
  'Black': { 'grams': 1906, 'price': 2.05},
  'Gold': { 'grams': 194, 'price': 8.24},
  'Orchid': { 'grams': 4970, 'price': 10.78},
  'Maroon': { 'grams': 922, 'price': 18.76},
  'Blue': { 'grams': 9526, 'price': 22.88},
  'Tan': { 'grams': 6738, 'price': 50.54},
  'Magenta': { 'grams': 6035, 'price': 56.69},
  'Mint green': { 'grams': 9961, 'price': 63.89}, 
}

【问题讨论】:

标签: python sorting dictionary hashtable


【解决方案1】:
for s in sorted(a.iteritems(), key=lambda (x, y): y['price']):
       print s

或者通过 OrderedDict

from collections import OrderedDict
res = OrderedDict(sorted(a.items(), key=lambda x: x[1]['price'], reverse=False))
print res

输出:

[('Black', {'price': 2.05, 'grams': 1906}), ('Gold', {'price': 8.24, 'grams': 194}), ('Orchid', {'price': 10.78, 'grams': 4970}), ('Maroon', {'price': 18.76, 'grams': 922}), ('Blue', {'price': 22.88, 'grams': 9526}), ('Tan', {'price': 50.54, 'grams': 6738}), ('Yellow', {'price': 54.19, 'grams': 6045}), ('Magenta', {'price': 56.69, 'grams': 6035}), ('Mint green', {'price': 63.89, 'grams': 9961})]

【讨论】:

    【解决方案2】:
    import collections
    
    update=collections.OrderedDict()
    result = sorted(a, key=lambda x: (a[x]['price']))
    for r in result:
        update[r]=a[r]
    
    print(update)
    

    【讨论】:

    • 成功了。但我在想必须有另一种方法将 for 循环与排序操作相结合以获得结果。
    • 既然需要对它们进行排序,我们需要使用 OrderedDict。在这种情况下,需要 IMO for 循环。
    【解决方案3】:

    从有序项元组列表中构造一个OrderedDict

    from collections import OrderedDict
    
    ordered = OrderedDict(sorted(a.items(), key=lambda i: i[1]['price']))
    

    .items() 假定 Python 3,在 Python 2 中 iteritems 应该这样做。)

    【讨论】:

      【解决方案4】:

      您还可以使用operator 库中的getitem

      from collections import OrderedDict
      from operator import getitem
      
      sorted_dict = OrderedDict(sorted(a.items(), key = lambda x:getitem(x[1],'price')))
      
      print(sorted_dict)
      

      输出:

      OrderedDict([('Black', {'grams': 1906, 'price': 2.05}), ('Gold', {'grams': 194, 'price': 8.24}), ('Orchid', {'grams': 4970, 'price': 10.78}), ('Maroon', {'grams': 922, 'price': 18.76}), ('Blue', {'grams': 9526, 'price': 22.88}), ('Tan', {'grams': 6738, 'price': 50.54}), ('Yellow', {'grams': 6045, 'price': 54.19}), ('Magenta', {'grams': 6035, 'price': 56.69}), ('Mint green', {'grams': 9961, 'price': 63.89})])
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-20
        相关资源
        最近更新 更多