python字典多条件排序方法实例
2014-06-30 00:00
1041 查看
项目编写过程中,总能遇见对字典进行排序什么的,如果要实现多条件排序只需要下面几行代码实现。充分体现了python的好处了。
以上代码实现了 按‘P',‘GD' ,‘GS' ,'GA' 四条件排序,reverse=True 表示降序
当然还可以
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中字典(dict)和列表(list)的排序方法实例
- Python中字典(dict)和列表(list)的排序方法实例
- Python字典按值排序、包含字典的列表按字典值排序的方法
- python字典(dictionary)使用:基本函数code实例,字典的合并、排序、copy,函数中*args 和**kwargs做形参和实参
- python list列表排序的两种方法及实例讲解
- python list排序的两种方法及实例讲解
- Python字典按值排序、包含字典的列表按字典值排序的方法
- python使用clear方法清除字典内全部数据实例
- Python字典按值排序、包含字典的列表按字典值排序的方法
- Python中对元组和列表按条件进行排序的方法示例
- python list排序的两种方法及实例讲解
- Python对列表排序的方法实例分析
- python对字典进行排序实例
- python list排序的两种方法及实例讲解
- Python字典按值排序、包含字典的列表按字典值排序的方法
- python字典(dictionary)使用:基本函数code实例,字典的合并、排序、copy,函数中*args 和**kwargs做形参和实参
- python排序方法实例分析
- Python对列表排序的方法实例分析
- Python字典按值排序、包含字典的列表按字典值排序的方法