排序
- sorted(iterable, key=None, reverse=False)
- list之sort()方法
- key最好用
```python
a = ['2354', '1234', '457345', '23']
a.sort(key = int) # 结果['23', '1234', '2354', '457345']
a = [('age1', 2), ('age2', 4), ('age3', 1)]
a.sort(key=lambda x:x[1], reverse=True)
```
- cmp是什么(a>b)-(a<b),python3中已经删除此方法和参数
```python
cmp(1,2) # -1
cmp(2,2) # 0
cmp(2,1) # 1
```
- 多级排序咋办
import operator
a = [(2,1,6), (4,8,2), (1,3,9)]
a.sort(key=operator.itemgetter(1,2)) # [(2, 1, 6), (1, 3, 9), (4, 8, 2)]
a = [(1,2,3), (2,3,4), (0,1,2)]
a.sort(key=operator.itemgetter(1,2)) # 所以这个是以游标1,也就是前面的为主
- 字典如何去排序
网友评论