Python列表按降序排列
我怎样才能以降序排列这个列表?
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" ]
timestamp.sort(key=lambda x: time.strptime(x, '%Y-%m-%d %H:%M:%S')[0:6], reverse=True)
这会给你一个数组的sorting版本。
sorted(timestamp, reverse=True)
如果你想在原地sorting:
timestamp.sort(reverse=True)
你可以简单地做到这一点:
timestamp.sort(reverse=True)
由于您的列表已经以升序排列,我们可以简单地反转列表。
>>> 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']
你简单的types:
timestamp.sort() timestamp=timestamp[::-1]