在OrderedDict中如何按特定属性排序? [英] In an OrderedDict how to sort by a particular attribute?

查看:80
本文介绍了在OrderedDict中如何按特定属性排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过执行以下操作对以下OrderedDict进行排序->

I am trying to sort the following OrderedDict by doing the following-->

>>> from collections import OrderedDict
>>> d = OrderedDict([(4995L, [{'isbn_13': u'9788131805923', 'book_quantity': 49L, 'seller_book_id': 4995L, 'book_id': 4995L, 'title': u'Industrial Automation and Robotics', 'selling_price': 292.0, 'id': 4995L, 'condition': 'New Book'}]), (6657L, [{'isbn_13': u'9788122425925', 'book_quantity': 49L, 'seller_book_id': 6657L, 'book_id': 6657L, 'title': u'A Textbook of Agricultural Statistics', 'selling_price': 243.0, 'id': 6657L, 'condition': 'New Book'}]), (6137L, [{'isbn_13': u'9788122425727\n', 'book_quantity': 50L, 'seller_book_id': 6137L, 'book_id': 6137L, 'title': u'A Guide to Corporate Governance', 'selling_price': 247.0, 'id': 6137L, 'condition': 'New Book'}]), (6260L, [{'isbn_13': u'9788122414394\n', 'book_quantity': 50L, 'seller_book_id': 6260L, 'book_id': 6260L, 'title': u'Management Accounting \n', 'selling_price': 269.0, 'id': 6260L, 'condition': 'New Book'}])])


>>> OrderedDict(sorted(d.items(), key=lambda item: item[1][0]['selling_price']))

通过 selling_price 属性.但是我做不到.

by the selling_price attribute. But I am not being able to do it.

我尝试应用此中讨论的概念?适用于正常的OrderedDict,但它不起作用.有人可以帮我吗?TIA

I tried to apply the concept discussed in this How to sort OrderedDict of OrderedDict? for normal OrderedDict but it did not work. Can someone please help me out? TIA

推荐答案

解决方案

这应该适用于有序词典 d :

OrderedDict(sorted(d.items(), key=lambda item: item[1][0]['selling_price']))

应用程序

>>> d
OrderedDict([(7484,
              [{'book_id': 7484,
                'book_quantity': 43,
                'condition': 'New Book',
                'id': 7484,
                'isbn_13': '9788131727591',
                'seller_book_id': 7484,
                'selling_price': 629.0,
                'title': 'Network Management:  Principles and Practice,  2/e'}]),
             (7485,
              [{'book_id': 7484,
                'book_quantity': 43,
                'condition': 'New Book',
                'id': 7484,
                'isbn_13': '9788131727591',
                'seller_book_id': 7484,
                'selling_price': 29.0,
                'title': 'Network Management:  Principles and Practice,  2/e'}])])

>>> OrderedDict(sorted(d.items(), key=lambda item: item[1][0]['selling_price']))
OrderedDict([(7485,
              [{'book_id': 7484,
                'book_quantity': 43,
                'condition': 'New Book',
                'id': 7484,
                'isbn_13': '9788131727591',
                'seller_book_id': 7484,
                'selling_price': 29.0,
                'title': 'Network Management:  Principles and Practice,  2/e'}]),
             (7484,
              [{'book_id': 7484,
                'book_quantity': 43,
                'condition': 'New Book',
                'id': 7484,
                'isbn_13': '9788131727591',
                'seller_book_id': 7484,
                'selling_price': 629.0,
                'title': 'Network Management:  Principles and Practice,  2/e'}])])

'selling_price':29.0 排序,小于'selling_price':629.0 .

这篇关于在OrderedDict中如何按特定属性排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆