python字典多条件排序方法实例


Posted in Python onJune 30, 2014

项目编写过程中,总能遇见对字典进行排序什么的,如果要实现多条件排序只需要下面几行代码实现。充分体现了python的好处了。

teamitems = [{'team':'France'     , 'P':1 , 'GD':-3 , 'GS':1 , 'GA':4},

            {'team':'Uruguay'     , 'P':7 , 'GD':4  , 'GS':4 , 'GA':0},

            {'team':'SouthAfrica' , 'P':4 , 'GD':-2 , 'GS':3 , 'GA':5},

            {'team':'Mexico'      , 'P':4 , 'GD':1  , 'GS':3 , 'GA':2}]
print sorted(teamitems ,key = lambda x:(x['P'],x['GD'],x['GS'],x['GA']),reverse=True)

以上代码实现了 按‘P',‘GD' ,‘GS' ,'GA' 四条件排序,reverse=True 表示降序

当然还可以

from operator import itemgetter

print sorted(teamitems ,key = itemgetter('P','GD','GS','GA'),reverse=True)
Python 相关文章推荐
Python验证码识别处理实例
Dec 28 Python
教大家玩转Python字符串处理的七种技巧
Mar 31 Python
Python针对给定字符串求解所有子序列是否为回文序列的方法
Apr 21 Python
解决安装tensorflow遇到无法卸载numpy 1.8.0rc1的问题
Jun 13 Python
python实现写数字文件名的递增保存文件方法
Oct 25 Python
python字符串分割及字符串的一些常规方法
Jul 24 Python
Python reversed函数及使用方法解析
Mar 17 Python
Python count函数使用方法实例解析
Mar 23 Python
Python 中由 yield 实现异步操作
May 04 Python
使用Python FastAPI构建Web服务的实现
Jun 08 Python
python可视化分析的实现(matplotlib、seaborn、ggplot2)
Feb 03 Python
Python中的tkinter库简单案例详解
Jan 22 Python
python实现的解析crontab配置文件代码
Jun 30 #Python
全面解读Python Web开发框架Django
Jun 30 #Python
Python科学计算环境推荐——Anaconda
Jun 30 #Python
9种python web 程序的部署方式小结
Jun 30 #Python
Python开发WebService系列教程之REST,web.py,eurasia,Django
Jun 30 #Python
windows下wxPython开发环境安装与配置方法
Jun 28 #Python
Python使用稀疏矩阵节省内存实例
Jun 27 #Python
You might like
PHP中的integer类型使用分析
2010/07/27 PHP
PHP __autoload()方法真的影响性能吗?
2012/03/30 PHP
smarty实现多级分类的方法
2014/12/05 PHP
THinkPHP获取客户端IP与IP地址查询的方法
2016/11/14 PHP
实例:尽可能写友好的Javascript代码
2006/10/09 Javascript
window.location.hash 属性使用说明
2010/03/20 Javascript
jQuery的deferred对象使用详解
2011/08/20 Javascript
JS实现图片高亮展示效果实例
2015/11/24 Javascript
学习Angular中作用域需要注意的坑
2016/08/17 Javascript
小程序开发实战:实现九宫格界面的导航的代码实现
2017/01/19 Javascript
jQuery插件Echarts实现的渐变色柱状图
2017/03/23 jQuery
angular动态删除ng-repaeat添加的dom节点的方法
2017/07/20 Javascript
jQuery实现合并表格单元格中相同行操作示例
2019/01/28 jQuery
js中复选框的取值及赋值示例详解
2020/10/18 Javascript
python使用三角迭代计算圆周率PI的方法
2015/03/20 Python
python基础入门学习笔记(Python环境搭建)
2016/01/13 Python
开源Web应用框架Django图文教程
2017/03/09 Python
Python信息抽取之乱码解决办法
2017/06/29 Python
对Python中DataFrame按照行遍历的方法
2018/04/08 Python
django请求返回不同的类型图片json,xml,html的实例
2018/05/22 Python
Python urllib request模块发送请求实现过程解析
2020/12/10 Python
HTML5和CSS3让网页设计提升到下一个高度
2009/08/14 HTML / CSS
潘多拉珠宝英国官方网上商店:PANDORA英国
2018/06/12 全球购物
LACOSTE波兰官网:Polo衫、服装和鞋类
2020/09/29 全球购物
物业管理大学生个人的自我评价
2013/10/10 职场文书
安全生产投入制度
2014/01/29 职场文书
超市开学活动方案
2014/03/01 职场文书
环保标语大全
2014/06/12 职场文书
2015年光棍节活动总结
2015/03/24 职场文书
2015年团支部年度工作总结
2015/05/27 职场文书
入党自传范文2015
2015/06/26 职场文书
培根随笔读书笔记
2015/07/01 职场文书
2016年小学教师政治学习心得体会
2016/01/23 职场文书
Go标准容器之Ring的使用说明
2021/05/05 Golang
Windows Server 2019 安装DHCP服务及相关配置
2022/04/28 Servers
SpringBoot接入钉钉自定义机器人预警通知
2022/07/15 Java/Android