0

日期保存格式为: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 ])
4

3 回答 3

3
sorted(feed, key=lambda x:x[7], reverse=True)
于 2012-11-01T00:46:26.477 回答
0

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

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)
于 2012-11-01T07:01:53.840 回答
0

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

sorted_feed = sorted(feed, key=lambda data: data[7], reverse=True)
于 2012-11-01T00:46:24.643 回答