我如何按降序排序这个列表?timestamp = [
"2010-04-20 10:07:30",
"2010-04-20 10:07:38",
"2010-04-20 10:07:52",
"2010-04-20 10:08:22",
"2010-04-20 10:08:22",
"2010-04-20 10:09:46",
"2010-04-20 10:10:37",
"2010-04-20 10:10:58",
"2010-04-20 10:11:50",
"2010-04-20 10:12:13",
"2010-04-20 10:12:13",
"2010-04-20 10:25:38"]
3 回答

拉风的咖菲猫
TA贡献1995条经验 获得超2个赞
lambda
:
timestamp.sort(key=lambda x: time.strptime(x, '%Y-%m-%d %H:%M:%S')[0:6], reverse=True)
list.sort
:
def foo(x): return time.strptime(x, '%Y-%m-%d %H:%M:%S')[0:6]timestamp.sort(key=foo, reverse=True)

繁星淼淼
TA贡献1775条经验 获得超11个赞
sorted(timestamp, reverse=True)
timestamp.sort(reverse=True)

守着一只汪
TA贡献1872条经验 获得超3个赞
由于您的列表已经按升序排列,我们可以简单地反转列表。
>>> timestamp.reverse()
>>> timestamp
['2010-04-20 10:25:38',
'2010-04-20 10:12:13',
'2010-04-20 10:12:13',
'2010-04-20 10:11:50',
'2010-04-20 10:10:58',
'2010-04-20 10:10:37',
'2010-04-20 10:09:46',
'2010-04-20 10:08:22',
'2010-04-20 10:08:22',
'2010-04-20 10:07:52',
'2010-04-20 10:07:38',
'2010-04-20 10:07:30']
添加回答
举报
0/150
提交
取消