I have an object with two attributes:
i.member : str
i.time : datetime
I tried to sort using:
l.sort(key=lambda i: (i.member, -i.time))
But it tells me that -
is invalid with datetime
objects (which makes sense). If this were a row in a Pandas data frame, I would sort df.sort_values(by=['member', 'time'], ascending=[True, False])
but I don't want to incur the overhead of loading it just for sorting.
I found previous questions on the topic but they only related to sorting by datetime
(but reversed) etc with the reverse
parameter, which only takes a boolean (regardless, it is sorting by tuple
so I can't pass a list of booleans).
How would I do this in normal Python?
假设您的日期都在 1970 年之后,您可以使用日期的时间戳值进行排序:
l.sort(key=lambda i: (i.member, -i.time.timestamp()))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.