简体   繁体   English

如何按键值和键值对字典列表进行排序?

[英]How to sort a list of dictionaries by the value of a key and by the value of a value of a key?

{
    "states": [
        {
            "timestamp": {
                "double": 968628281.0
            },
            "sensorSerialNumber": 13020235
        },
        {
            "timestamp": {
                "double": 964069109.0
            },
            "sensorSerialNumber": 13020203
        },
        {
            "timestamp": {
                "double": 9641066.0
            },
            "sensorSerialNumber": 30785
        }
    ]
}

Is there a way to sort this list of dictionaries by "sensorSerialNumber" and by this number inside the value of "timestamp" (9.68628281E8), 有没有一种方法可以通过“ sensorSerialNumber”和“ timestamp”(9.68628281E8)值中的此数字对字典列表进行排序,

"timestamp":{"double":9.68628281E8}

using the built-in function( sorted ) 使用内置函数(已排序

from operator import itemgetter
newlist = sorted(list_to_be_sorted, key=itemgetter('name'))

However, my question is slightly different from other questions . 但是,我的问题与其他问题略有不同 If there wasn't a dictionary inside the dictionary I would just need to add the string of the second key to the built-in function, like: 如果字典中没有字典,则只需将第二个键的字符串添加到内置函数中,例如:

from operator import itemgetter
newlist = sorted(list_to_be_sorted, key=itemgetter('name', 'age'))

key can be any callable, it is passed each element in the list_to_be_sorted in turn. key可以是任何可调用key ,它依次传递给list_to_be_sorted中的每个元素。 So a lambda would fit here: 因此, lambda将适合此处:

newlist = sorted(
    data['states'],
    key=lambda i: (i['sensorSerialNumber'], i['timestamp']['double']))

So the lambda returns the value for the 'sensorSerialNumber' key as the first element of a tuple, and the value for the 'double' key from the dictionary value for the 'timestamp' key. 因此,lambda返回'sensorSerialNumber'键的值作为元组的第一个元素,并从'timestamp'键的字典值中返回'double'键的值。

Demo: 演示:

>>> data = {"states" :
... [{"timestamp":{"double":9.68628281E8},"sensorSerialNumber":13020235},
... {"timestamp":{"double":9.64069109E8},"sensorSerialNumber":13020203},
... {"timestamp":{"double":9641066.0},"sensorSerialNumber":30785}]
... }
>>> sorted(data['states'], key=lambda i: (i['sensorSerialNumber'], i['timestamp']['double']))
[{'timestamp': {'double': 9641066.0}, 'sensorSerialNumber': 30785}, {'timestamp': {'double': 964069109.0}, 'sensorSerialNumber': 13020203}, {'timestamp': {'double': 968628281.0}, 'sensorSerialNumber': 13020235}]
>>> from pprint import pprint
>>> pprint(_)
[{'sensorSerialNumber': 30785, 'timestamp': {'double': 9641066.0}},
 {'sensorSerialNumber': 13020203, 'timestamp': {'double': 964069109.0}},
 {'sensorSerialNumber': 13020235, 'timestamp': {'double': 968628281.0}}]

The demo is not that meaningful, as there are no equal serial numbers for the inclusion of the timestamp value to make a difference. 该演示没有那么有意义,因为没有相等的序列号来包含时间戳记值以产生影响。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM