python 按自定义lambda函数排序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 按自定义lambda函数排序相关的知识,希望对你有一定的参考价值。

import datetime

CURRENT_DATE = datetime.date.today()
DAY_DELTA = datetime.timedelta(days=1)

END_DATE = CURRENT_DATE - DAY_DELTA
START_DATE = END_DATE - datetime.timedelta(days=END_DATE.weekday())

SEASON_DATES = [
    ("jan", datetime.date(END_DATE.year, 1, 1)),
    ("apr", datetime.date(END_DATE.year - 1 if END_DATE.month < 4 else END_DATE.year, 4, 1)),
    ("may", datetime.date(END_DATE.year - 1 if END_DATE.month < 5 else END_DATE.year, 5, 1)),
    ("jul", datetime.date(END_DATE.year - 1 if END_DATE.month < 7 else END_DATE.year, 7, 1)),
    ("sep", datetime.date(END_DATE.year - 1 if END_DATE.month < 9 else END_DATE.year, 9, 1)),
    ("oct", datetime.date(END_DATE.year - 1 if END_DATE.month < 10 else END_DATE.year, 10, 1)),
    ("week", START_DATE)
]
SEASON_SEQUENCE = sorted(SEASON_DATES, reverse=True, key=lambda elem: (END_DATE - elem[1]).days)

以上是关于python 按自定义lambda函数排序的主要内容,如果未能解决你的问题,请参考以下文章

在 Eloquent 中按自定义顺序对集合进行排序 [重复]

php 标签按自定义字段排序标记排序

按自定义字典顺序对字符串进行排序

MongoDB按自定义字段排序集合

按自定义模式排序列

按自定义数组对行进行排序