小编Ale*_*yuk的帖子

基于日期部分反转的 Python 列表排序列表

我有个问题。我有一个看起来像这样的列表:

[
[datetime.date(2019, 3, 29), Decimal('44819.75')],
[datetime.date(2019, 3, 29), Decimal('45000.00')],
[datetime.date(2019, 3, 28), Decimal('0.00')],
[datetime.date(2019, 3, 22), Decimal('-275.00')],
[datetime.date(2019, 3, 22), Decimal('-350.00')],
[datetime.date(2019, 3, 22), Decimal('-175.00')]
]
Run Code Online (Sandbox Code Playgroud)

我需要在日期字段(第一个)上进行排序,但每组相同的日期必须以相反的顺序排序。结果列表必须如下所示:

[
[datetime.date(2019, 3, 29), Decimal('45000.00')],
[datetime.date(2019, 3, 29), Decimal('44819.75')],
[datetime.date(2019, 3, 28), Decimal('0.00')],
[datetime.date(2019, 3, 22), Decimal('-175.00')],
[datetime.date(2019, 3, 22), Decimal('-350.00')],
[datetime.date(2019, 3, 22), Decimal('-275.00')],
]
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,列表是按日期排序的,但是对于相同的日期,列表是相反的。

日期仍在下降 2019-3-29 2019-3-28 2019-3-22 但对于每个日期,如果该日期有 1 个以上的元素,则项目会颠倒。

2019-3-29 有 2 个元素

[datetime.date(2019, 3, 29), Decimal('44819.75')],
[datetime.date(2019, 3, 29), Decimal('45000.00')],
Run Code Online (Sandbox Code Playgroud)

并在结果列表中的列表顺序颠倒

[datetime.date(2019, 3, 29), Decimal('45000.00')],
[datetime.date(2019, …
Run Code Online (Sandbox Code Playgroud)

python sorting datetime

5
推荐指数
1
解决办法
570
查看次数

标签 统计

datetime ×1

python ×1

sorting ×1