简体   繁体   English

根据另一个嵌套字典的最后一个值,用字典对 python 列表进行排序

[英]Sort python list with dictionaries based on the last value of another nested dictionary

I have a complex list with dictionaries which consist of other dictionaries.我有一个复杂的列表,其中包含由其他词典组成的词典。 I need to sort the following list according to the last y value so that the USD data comes first after AUD data.我需要根据最后一个y值对以下列表进行排序,以便美元数据排在澳元数据之后。 The last of value for USD is 4 and the last value of AUD is 3, so USD should be the first dictionary in the list and AUD should be the second dictionary in the list. USD 的最后一个值是 4,AUD 的最后一个值是 3,所以 USD 应该是列表中的第一个字典,AUD 应该是列表中的第二个字典。

I would appreciate any advice on how this can be achieved.我将不胜感激有关如何实现这一目标的任何建议。

Here is the original list:这是原始列表:

[
     {
      "instrument": "AUD",
      "changes": [
                  {"x": 1632765600000, "y": 0},
                  {"x": 1632769200000, "y": 1},
                  {"x": 1632772800000, "y": 2},
                  {"x": 1632776400000, "y": 3},
      ]
     },
     {
      "instrument": "USD",
      "changes": [
                  {"x": 1632765600000, "y": 0},
                  {"x": 1632769200000, "y": 2},
                  {"x": 1632772800000, "y": 3},
                  {"x": 1632776400000, "y": 4},
      ]
     },
]

This is the result I'm willing to obtain:这是我愿意得到的结果:

[
     {
      "instrument": "USD",
      "changes": [
                  {"x": 1632765600000, "y": 0},
                  {"x": 1632769200000, "y": 2},
                  {"x": 1632772800000, "y": 3},
                  {"x": 1632776400000, "y": 4},
      ]
     },
     {
      "instrument": "AUD",
      "changes": [
                  {"x": 1632765600000, "y": 0},
                  {"x": 1632769200000, "y": 1},
                  {"x": 1632772800000, "y": 2},
                  {"x": 1632776400000, "y": 3},
      ]
     },
]

see below (using lambda as a key)见下文(使用lambda作为密钥)

data = [
     {
      "instrument": "AUD",
      "changes": [
                  {"x": 1632765600000, "y": 0},
                  {"x": 1632769200000, "y": 1},
                  {"x": 1632772800000, "y": 2},
                  {"x": 1632776400000, "y": 3},
      ]
     },
     {
      "instrument": "USD",
      "changes": [
                  {"x": 1632765600000, "y": 0},
                  {"x": 1632769200000, "y": 2},
                  {"x": 1632772800000, "y": 3},
                  {"x": 1632776400000, "y": 4},
      ]
     }
]

data = sorted(data, key = lambda x : x['changes'][-1]['y'],reverse=True)
print()
print(data)
print()

output output

[{'instrument': 'USD', 'changes': [{'x': 1632765600000, 'y': 0}, {'x': 1632769200000, 'y': 2}, {'x': 1632772800000, 'y': 3}, {'x': 1632776400000, 'y': 4}]}, {'instrument': 'AUD', 'changes': [{'x': 1632765600000, 'y': 0}, {'x': 1632769200000, 'y': 1}, {'x': 1632772800000, 'y': 2}, {'x': 1632776400000, 'y': 3}]}]

暂无
暂无

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

相关问题 访问python中的字典列表并使用值对其进行排序(嵌套字典) - Accessing list of dictionaries in python and sort it using value (Nested dictionary) 如何在Python中按字典的姓氏对字典列表进行排序? - How do I sort a list of dictionaries by last name of the dictionary in Python? 根据对另一个嵌套字典 python 所做的检查创建嵌套字典 - Creating nested dictionary based on the checks done on another nested dictionaries python 如何按嵌套字典中的值对字典列表进行排序? - How do I sort a list of dictionaries by a value within a nested dictionary? Python如何通过字典键对嵌套字典列表进行排序? - Python how to sort a list of nested dictionaries by dictionary keys? 根据基于该字典中另一个键的值的条件,更新python词典列表中的值 - Update a value in a list of dictionaries in python based on a condition based on the value of another key in that dictionary Python:根据值的出现对字典列表中的特定字典对进行排序 - Python: sort specific pairs of dictionaries in a list of dictionaries based on occurrence of value 根据 python 中嵌套字典的键值从列表中删除字典 - remove dictionaries from list based on key value from nested dictionary in python 按值对嵌套字典列表进行排序 - Sort list of nested dictionaries by value 如何根据字典列表中的另一个值有效地查找字典值 - How to efficiently find a dictionary value based on another value in a list of dictionaries
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM