如何按日期对列表进行排序?

时间:2012-11-01 00:42:27

标签: python list date sorting

日期保存格式为:2012-09-28。如何对列表进行排序,以便最近发布的项目在列表中排在第一位?

feed = []
for entry in entries:
        #code that saves title, desc, thumbnail, video, author, url, length, and date
        feed.append([title, desc, thumbnail, video, author, url, length, date ])

3 个答案:

答案 0 :(得分:3)

sorted(feed, key=lambda x:x[7], reverse=True)

答案 1 :(得分:0)

您很幸运,日期已经采用可排序的格式。只需选择适当的字段进行排序即可。

sorted_feed = sorted(feed, key=lambda data: data[7], reverse=True)

答案 2 :(得分:0)

我会这样做以避免创建Feed的中间副本:

def parsed_entries(entries):
    for entry in entries:
        # code that extracts fields from an entry...
        yield [title, desc, thumbnail, video, author, url, length, date]

feed = sorted(parsed_entries, key=lambda entry: entry[7], reversed=True)