python排序方法实例分析,python排序实例分析
python排序方法实例分析,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实现TCP服务器端与客户端的方法详解,pythontcp
- python获取当前日期和时间的方法,python获取当前日期
- Python的Urllib库的基本使用教程,pythonurllib
- python插入数据到列表的方法,python插入数据列表
- 介绍Python的Urllib库的一些高级用法,pythonurllib库
- 处理Python中的URLError异常的方法,pythonurlerror
- 详解在Python程序中使用Cookie的教程,pythoncookie
- Python中正则表达式的详细教程,python正则表达式
- Python中使用Beautiful Soup库的超详细教程,pythonsoup
- Python的Bottle框架中实现最基本的get和post的方法的教程,
评论关闭