python排序方法实例分析
2015-04-30 00:00
726 查看
本文实例讲述了python排序方法。分享给大家供大家参考。具体如下:
希望本文所述对大家的Python程序设计有所帮助。
>>> def my_key1(x): ... return x % 10 ... >>> alist = [4, 5, 8, 1, 63, 8] >>> alist [4, 5, 8, 1, 63, 8] >>> alist.sort() # 默认升序排序 >>> alist [1, 4, 5, 8, 8, 63] >>> alist.sort(reverse = True) # 改为降序排序 >>> alist [63, 8, 8, 5, 4, 1] >>> alist.sort(key = my_key1) # 设置排序的key值 >>> alist [1, 63, 4, 5, 8, 8] >>> >>> def my_key2(x): ... return x[1] ... >>> alist = [(5,'a'),(1,'w'),(2,'e'),(6,'f')] >>> alist.sort(key = my_key2) # 根据每个元组的第二分量进行排序 >>> alist [(5, 'a'), (2, 'e'), (6, 'f'), (1, 'w')] >>>
希望本文所述对大家的Python程序设计有所帮助。
相关文章推荐
- python排序方法实例分析
- Python对列表排序的方法实例分析
- Python对列表排序的方法实例分析
- Python类的专用方法实例分析
- Python中的对象,方法,类,实例,函数用法分析
- python私有属性和方法实例分析
- python私有属性和方法实例分析
- Python中的对象,方法,类,实例,函数用法分析
- python中base64加密解密方法实例分析
- Python3写入文件常用方法实例分析
- Python3读取文件常用方法实例分析
- Python3读取文件常用方法实例分析
- Python3写入文件常用方法实例分析
- python避免死锁方法实例分析
- python避免死锁方法实例分析
- python实现数值积分的Simpson方法实例分析
- python实现数值积分的Simpson方法实例分析
- python读写ini配置文件方法实例分析
- python读写ini配置文件方法实例分析