0.前言

python3.6

python3 为什么取消了sort方法中的cmp参数?

1.排序

待排序的list如下:

print(all_list)
>>>[[{'name': 'article'}, {'id': 35}, {'time': datetime.datetime(2017, 11, 20, 8, 51, 7)}], [{'name': 'article'}, {'id': 34}, {'time': datetime.datetime(2017, 11, 20, 7, 46, 39)}], [{'name': 'article'}, {'id': 33}, {'time': datetime.datetime(2017, 11, 15, 1, 27, 36)}], [{'name': 'article'}, {'id': 31}, {'time': datetime.datetime(2017, 11, 7, 3, 47, 38)}], [{'name': 'article'}, {'id': 30}, {'time': datetime.datetime(2017, 11, 1, 1, 22, 50)}], [{'name': 'article'}, {'id': 29}, {'time': datetime.datetime(2017, 10, 30, 0, 34, 2)}], [{'name': 'article'}, {'id': 28}, {'time': datetime.datetime(2017, 10, 28, 5, 38, 29)}], [{'name': 'article'}, {'id': 25}, {'time': datetime.datetime(2017, 10, 27, 23, 57, 38)}], [{'name': 'article'}, {'id': 24}, {'time': datetime.datetime(2017, 10, 27, 2, 22, 28)}], [{'name': 'article'}, {'id': 23}, {'time': datetime.datetime(2017, 10, 27, 2, 9, 26)}], [{'name': 'essay'}, {'id': 124}, {'time': datetime.datetime(2017, 11, 19, 10, 31, 3)}], [{'name': 'essay'}, {'id': 123}, {'time': datetime.datetime(2017, 11, 13, 2, 28, 6)}], [{'name': 'essay'}, {'id': 122}, {'time': datetime.datetime(2017, 11, 12, 6, 6, 54)}], [{'name': 'essay'}, {'id': 121}, {'time': datetime.datetime(2017, 11, 4, 14, 43, 53)}], [{'name': 'essay'}, {'id': 118}, {'time': datetime.datetime(2017, 11, 1, 9, 15, 32)}]]

可见,每个子列表内包含着这个子列表的来源是文章article还是随笔essay、它的id、它的创建时间,而排序的key就是time的value。

如果time内是字符串,需要另外转成datatime.datetime,此处是直接从Mysql提取出来的所以可以直接比较。

all_list.sort(key=lambda x:x[2].get('time'),reverse=True)
print(all_list)
>>>[[{'name': 'article'}, {'id': 35}, {'time': datetime.datetime(2017, 11, 20, 8, 51, 7)}], [{'name': 'article'}, {'id': 34}, {'time': datetime.datetime(2017, 11, 20, 7, 46, 39)}], [{'name': 'essay'}, {'id': 124}, {'time': datetime.datetime(2017, 11, 19, 10, 31, 3)}], [{'name': 'article'}, {'id': 33}, {'time': datetime.datetime(2017, 11, 15, 1, 27, 36)}], [{'name': 'essay'}, {'id': 123}, {'time': datetime.datetime(2017, 11, 13, 2, 28, 6)}], [{'name': 'essay'}, {'id': 122}, {'time': datetime.datetime(2017, 11, 12, 6, 6, 54)}], [{'name': 'article'}, {'id': 31}, {'time': datetime.datetime(2017, 11, 7, 3, 47, 38)}], [{'name': 'essay'}, {'id': 121}, {'time': datetime.datetime(2017, 11, 4, 14, 43, 53)}], [{'name': 'essay'}, {'id': 118}, {'time': datetime.datetime(2017, 11, 1, 9, 15, 32)}], [{'name': 'article'}, {'id': 30}, {'time': datetime.datetime(2017, 11, 1, 1, 22, 50)}], [{'name': 'article'}, {'id': 29}, {'time': datetime.datetime(2017, 10, 30, 0, 34, 2)}], [{'name': 'article'}, {'id': 28}, {'time': datetime.datetime(2017, 10, 28, 5, 38, 29)}], [{'name': 'article'}, {'id': 25}, {'time': datetime.datetime(2017, 10, 27, 23, 57, 38)}], [{'name': 'article'}, {'id': 24}, {'time': datetime.datetime(2017, 10, 27, 2, 22, 28)}], [{'name': 'article'}, {'id': 23}, {'time': datetime.datetime(2017, 10, 27, 2, 9, 26)}]]

那么就按照时间的先后来排完序了。其实很简单,因为这里的数据都很规整,以后遇到难得再来补。

3.P.S

all_list是两个list拼一起的,用list.extend()