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': 7, 'GD': 4, 'GS': 4, 'GA': 0, 'team': 'Uruguay'},
{'P': 4, 'GD': 1, 'GS': 3, 'GA': 2, 'team': 'Mexico'},
{'P': 4, 'GD': -2, 'GS': 3, 'GA': 5, 'team': 'SouthAfrica'},
{'P': 1, 'GD': -3, 'GS': 1, 'GA': 4, 'team': 'France'}]
from operator import itemgetter
print sorted(teamitems ,key = itemgetter('P','GD','GS','GA'),reverse=True)
#假设数据如下。
data = '''''
区服,玩家id,累积消费
3,a,2380
1,b,11900
4,e,3250
1,k,100
4,j,599
2,m,872
3,f,5560
1,y,2500
'''
items = [x.split(',') for x in filter(None,data.split('\n'))[1:]] #去掉空行和忽略首行并把字符串转成二维数组
#方法一
items.sort(key=lambda x:int(x[2]),reverse=True)#先排消费
items.sort(key=lambda x:int(x[0]))#然后排区服
print '\n'.join([','.join(x) for x in items])
print '-----------'
#方法二
items = sorted(items,key=lambda x:(int(x[0]),-int(x[2])))
print '\n'.join([','.join(x) for x in items])
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有