代码之家  ›  专栏  ›  技术社区  ›  S Andrew

如何在Python中根据dict的日期时间对其进行排序

  •  0
  • S Andrew  · 技术社区  · 5 年前

    [
        {
            "attributes": 
            [
                {
                    "label": "Event Date",
                    "value": "Tue, 29 Jun 2021 18:30:00 GMT"
                }
            ],
            "event": "DT2"
        },
        {
            "attributes": 
            [
                {
                    "label": "DT3",
                    "value": true
                },
                {
                    "label": "Event Date",
                    "value": "Thu, 22 Jul 2021 18:30:00 GMT"
                }
            ],
            "event": "DT5"
        },
        {
            "attributes": [
                {
                    "label": "DT6",
                    "value": 1.0
                },
                {
                    "label": "Event Date",
                    "value": "Thu, 08 Jul 2021 18:30:00 GMT"
                }
            ],
            "event": "DT7"
        }
    ]
        
    

    list 属于 dict 每个 字典 有一个 列表 attributes . attriutes Event Date .我必须根据的升序对列表中的所有词典进行排序 事件日期

    0 回复  |  直到 5 年前
        1
  •  1
  •   Viliam Vadocz    5 年前

    您希望按键排序。

    my_list = [
        {
            "foo": 5,
            "bar": 4,
        },
        {
            "foo": 3,
            "bar": 2,
        },
        {
            "foo": 1,
            "bar": 0,
        },
    ]
    
    # This will sort the list according to the "foo" in each dictionary.
    my_list.sort(key=lambda dictionary: dictionary["foo"])
    

    对于您的特定情况,由于它更复杂,我将创建一个函数,而不是使用lambda表达式。此外,我将使用 datetime 来帮助我比较日期字符串。

    from datetime import datetime
    
    def key_function(dictionary):
        attributes = dictionary["attributes"]
        for attribute in attributes:
            if attribute["label"] == "Event Date":
                date_string = attribute["value"]
                date = datetime.strptime(date_string, "%a, %d %b %Y %X %Z")
                # Compare dates according to their POSIX timestamp
                # I am assuming the dates will be after 1970
                return date.timestamp()
        raise ValueError("Event Date attribute wasn't found")
    
    my_list.sort(key=key_function)
    
        2
  •  1
  •   jizhihaoSAMA    5 年前

    您需要首先获取日期时间字符串,然后格式化日期时间字符串:

    import datetime
    
    lst = [
        {
            "attributes": 
            [
                {
                    "label": "Event Date",
                    "value": "Tue, 29 Jun 2021 18:30:00 GMT"
                }
            ],
            "event": "DT2"
        },
        {
            "attributes": 
            [
                {
                    "label": "DT3",
                    "value": True
                },
                {
                    "label": "Event Date",
                    "value": "Thu, 22 Jul 2021 18:30:00 GMT"
                }
            ],
            "event": "DT5"
        },
        {
            "attributes": [
                {
                    "label": "DT6",
                    "value": 1.0
                },
                {
                    "label": "Event Date",
                    "value": "Thu, 08 Jul 2021 18:30:00 GMT"
                }
            ],
            "event": "DT7"
        }
    ]
        
    lst.sort(key=lambda x: datetime.datetime.strptime(next(attr["value"] for attr in x["attributes"] if attr["label"] == "Event Date"), "%a, %d %b %Y %X %Z"))
    

    def check(x):
        for attr in x["attributes"]:
            if attr["label"] == "Event Date":
                return datetime.datetime.strptime(attr["value"], "%a, %d %b %Y %X %Z")
     
    lst.sort(key=check)
    

    给了我:

    [{'attributes': [{'label': 'Event Date',
        'value': 'Tue, 29 Jun 2021 18:30:00 GMT'}],
      'event': 'DT2'},
     {'attributes': [{'label': 'DT6', 'value': 1.0},
       {'label': 'Event Date', 'value': 'Thu, 08 Jul 2021 18:30:00 GMT'}],
      'event': 'DT7'},
     {'attributes': [{'label': 'DT3', 'value': True},
       {'label': 'Event Date', 'value': 'Thu, 22 Jul 2021 18:30:00 GMT'}],
      'event': 'DT5'}]