当前位置:  开发笔记 > 编程语言 > 正文

在OrderedDict中如何按特定属性排序?

如何解决《在OrderedDict中如何按特定属性排序?》经验,为你挑选了1个好方法。

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

>>> 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属性.但我无法做到这一点.

我尝试应用这个如何对OrderedDict的OrderedDict进行排序的概念-用于普通OrderedDict的Python,但它没有用.有人可以帮帮我吗?



1> Mike Müller..:

这适用于有序字典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.

推荐阅读
大大炮
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有