按日期排序Python对象列表 [英] Sort Python list of objects by date

查看:451
本文介绍了按日期排序Python对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为results的Python列表。结果列表中的每个结果都有一个person对象,每个person对象都有一个birthdate(result.person.birthdate)。 birthdate是一个datetime对象。



我想按birthdate以最早的排序排列列表。最简单的Pythonic方法是什么?

解决方案

  results.sort(key = lambda r:r.person.birthdate)


I have a Python list called results. Each result in the results list has a person object, and each person object has a birthdate (result.person.birthdate). The birthdate is a datetime object.

I would like to order the list by birthdate with the oldest first. What is the most Pythonic way to do this?

解决方案

results.sort(key=lambda r: r.person.birthdate)

这篇关于按日期排序Python对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆