简体   繁体   English

根据多个字典列表的键对数据进行排序

[英]Sort data based on key of a list of multiple dictionaries

I have a list like this 我有这样的清单

li = [
    {1: {'amount': 255, 'date': '25-02-2017'}},
    {2: {'amount': 25, 'date': '2-02-2017'}},
    {3: {'amount': 38, 'date': '20-02-2017'}},
]

I am trying to sort the data in ascending and descending order based on 'amount' . 我试图基于'amount'来对数据进行升序和降序排序。

I tried the following but am unable to get the result I want: 我尝试了以下操作,但无法获得想要的结果:

import operator
li.sort(key=operator.itemgetter('amount'))

and

from operator import itemgetter
newlist = sorted(li, key=itemgetter('amount'))

Both raise an exception, KeyError: 'amount' . 两者都引发异常KeyError: 'amount'

You have a list of dictionaries with a single key, whose value is another dictionary. 您有一个带有单个键的字典列表,其值是另一个字典。 It is that nested dictionary that has the key 'amount' . 那个嵌套字典具有键'amount'

You'd have to reach in and get that single value: 您必须达到并获得该单一价值:

newlist = sorted(li, key=lambda d: d.values()[0]['amount'])

This only works in Python 2, where d.values() is a list. 这仅在Python 2中有效,其中d.values()是列表。 In Python 3 you'd have to use next(iter(d.values())['amount'] instead. 在Python 3中,您必须使用next(iter(d.values())['amount']

You'd be much better of not producing the nested structure at all . 你一定会觉得很没有产生嵌套结构在所有的更好。 Move those numbered keys into another key-value pair perhaps: 将那些编号的键移动到另一个键值对中:

li = [
    {'id': 1', amount': 255, 'date': '25-02-2017'},
    {'id': 2, 'amount': 25, 'date': '2-02-2017'},
    {'id': 3, 'amount': 38, 'date': '20-02-2017'},
]

at which point your original attempt would work. 在这一点上,您最初的尝试会奏效。 You can transform your current data structure with [dict(id=d.keys()[0], **d.values()[0]) for d in li] but it'd be better to fix the code that produced the list. 您可以使用[dict(id=d.keys()[0], **d.values()[0]) for d in li]转换当前数据结构,但最好修复产生的代码名单。

Try the following code which uses lambdas, 请尝试以下使用lambda的代码,

li=[{1:{'amount':255,'date':'25-02-2017'}},{2:{'amount':25,'date':'2-02-2017'}},{3:{'amount':38,'date':'20-02-2017'}}] 
print(li)

li.sort(key=lambda x:list(x.values())[0]['amount']) # ascending
print(li)

li.sort(key=lambda x:list(x.values())[0]['amount'],reverse=True) # descending
print(li)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据Python中单独列表中键的顺序,按键对字典列表进行排序 - Sort a list of dictionaries by a key, based on the order of keys in a separate list in Python 如何根据深度嵌套的键对嵌套字典列表进行排序? - How to sort a list of nested dictionaries based on a deeply nested key? 根据列表定义的特定顺序,以相同方式对多个词典进行排序 - Sort multiple dictionaries identically, based on a specific order defined by a list Python:根据多个键值过滤字典列表 - Python: filter list of dictionaries based on multiple key values 如何根据给定的键列在列表中组合多个词典? - How to combine multiple dictionaries in a list based on the given key columns? 如果密钥存在,则按密钥对字典的字典列表进行排序 - Sort python list of dictionaries by key if key exists 什么是基于多个键对词典列表进行排序并删除某些词典的最佳方法? - What is the optimal way to sort a list of dictionaries based on multiple keys and remove some of the dictionaries 基于键的字典组列表 - Group list of dictionaries based on key 如何在 Python 中不使用 sort() 根据某个键的值对字典列表进行排序 - How to sort a list of dictionaries based on the value of one certain key without using sort() in Python 根据键对字典列表取平均值 - Average a list of dictionaries based on key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM