日期保存格式为: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 ])
sorted(feed, key=lambda x:x[7], reverse=True)
我会这样做以避免创建提要的中间副本:
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)
您很幸运,日期已经采用可排序的格式。只需选择适当的字段进行排序即可。
sorted_feed = sorted(feed, key=lambda data: data[7], reverse=True)