标签:
使用列表的sort方法可以进行排序,其中有两个参数用来表示排序的方式,代码:
In [7]: a = [‘x11‘,‘abc323‘,‘e26‘,‘112ddd‘] In [8]: a.sort(key=len, reverse=True) In [9]: a Out[9]: [‘abc323‘, ‘112ddd‘, ‘x11‘, ‘e26‘] In [10]: a.sort(key=lambda x:x[-1]) In [11]: a Out[11]: [‘x11‘, ‘abc323‘, ‘e26‘, ‘112ddd‘] In [12]: a = zip(range(10), range(10)[::-1]) In [13]: a Out[13]: <zip at 0x518d308> In [14]: b = list(a) In [15]: b Out[15]: [(0, 9), (1, 8), (2, 7), (3, 6), (4, 5), (5, 4), (6, 3), (7, 2), (8, 1), (9, 0)] In [17]: b.sort(key=lambda x:x[1]) In [18]: b Out[18]: [(9, 0), (8, 1), (7, 2), (6, 3), (5, 4), (4, 5), (3, 6), (2, 7), (1, 8), (0, 9)]
参数key可以是:key=int,key=len, key=lambda...
python列表sort方法的两个参数key, reverse
标签:
原文地址:http://www.cnblogs.com/Raymon-Geng/p/5844212.html