@Tiran_94
Python Django

Как отсортировать лист дат по возрастанию?

Есть лист дат, как его можно отсортировать по возрастанию?
  • Вопрос задан
  • 185 просмотров
Пригласить эксперта
Ответы на вопрос 1
@d1skort
junior
In [1]: import datetime

In [2]: L = []

In [3]: for i in range(10):
   ...:     L.append(datetime.datetime.now() - datetime.timedelta(days=i))
   ...:     

In [4]: L
Out[4]: 
[datetime.datetime(2017, 3, 20, 13, 23, 21, 840827),
 datetime.datetime(2017, 3, 19, 13, 23, 21, 840862),
 datetime.datetime(2017, 3, 18, 13, 23, 21, 840871),
 datetime.datetime(2017, 3, 17, 13, 23, 21, 840877),
 datetime.datetime(2017, 3, 16, 13, 23, 21, 840883),
 datetime.datetime(2017, 3, 15, 13, 23, 21, 840888),
 datetime.datetime(2017, 3, 14, 13, 23, 21, 840894),
 datetime.datetime(2017, 3, 13, 13, 23, 21, 840899),
 datetime.datetime(2017, 3, 12, 13, 23, 21, 840904),
 datetime.datetime(2017, 3, 11, 13, 23, 21, 840909)]

In [5]: L.sort()

In [6]: L
Out[6]: 
[datetime.datetime(2017, 3, 11, 13, 23, 21, 840909),
 datetime.datetime(2017, 3, 12, 13, 23, 21, 840904),
 datetime.datetime(2017, 3, 13, 13, 23, 21, 840899),
 datetime.datetime(2017, 3, 14, 13, 23, 21, 840894),
 datetime.datetime(2017, 3, 15, 13, 23, 21, 840888),
 datetime.datetime(2017, 3, 16, 13, 23, 21, 840883),
 datetime.datetime(2017, 3, 17, 13, 23, 21, 840877),
 datetime.datetime(2017, 3, 18, 13, 23, 21, 840871),
 datetime.datetime(2017, 3, 19, 13, 23, 21, 840862),
 datetime.datetime(2017, 3, 20, 13, 23, 21, 840827)]
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы