# -*- coding: utf-8 -*- #coding=utf-8 ''' @author: tomcat @license: (C) Copyright 2017-2019, Personal exclusive right. @contact: liliang07@yungengxin.com @software: coding @file: sorted.py @time: 2019/7/24 16:27 ''' ''' sorted()函数是一个高阶函数,它还可以接收一个key函数来实现自定义的排序 ''' a=sorted(['bob', 'about', 'Zoo', 'Credit'], key=lambda x:x.lower(), reverse=True) print(a) a=sorted(['bob', 'about', 'Zoo', 'Credit'], reverse=True) print(a) ''' 忽略字母大小写,进行名字排序 ''' params_original = sorted( [('bob', 75), ('adam', 92), ('cart', 66), ('Eisa', 88)], key = lambda a:a[0].lower()) print("忽略字母大小写,进行名字排序"+str(params_original)) ''' 按成绩从低到高排序: ''' params_original = sorted( [('bob', 75), ('adam', 92), ('cart', 66), ('Eisa', 88)], key = lambda a:a[1]) print("按成绩从低到高排序:"+str(params_original)) ''' 按成绩从高到低排序: ''' params_original = sorted( [('bob', 75), ('adam', 92), ('cart', 66), ('Eisa', 88)], key = lambda a:a[1],reverse=True) print("按成绩从高到低排序:"+str(params_original)) ''' 忽略字母大小写,对req字典按key排序 ''' req = {"channel":"guanwang","systemOs":"vivo","clientVer":"1.3.1","clientId":"5b3b4510e48cc6703a96102f19beec1b","loginPlatform":"android","loginToken":"","timeStamp":1563434953,"sign":"4efb209fb3021e09f737f3dc4a6ddec4"} params_original = sorted( req.items(), key = lambda a:a[0].lower()) print("忽略字母大小写,对req字典按key排序:"+str(params_original))
['bob', 'about', 'Zoo', 'Credit']
忽略字母大小写,进行名字排序[('adam', 92), ('bob', 75), ('cart', 66), ('Eisa', 88)]
按成绩从低到高排序:[('cart', 66), ('bob', 75), ('Eisa', 88), ('adam', 92)]
按成绩从高到低排序:[('adam', 92), ('Eisa', 88), ('bob', 75), ('cart', 66)]
忽略字母大小写,对req字典按key排序:[('channel', 'guanwang'), ('clientId', '5b3b4510e48cc6703a96102f19beec1b'), ('clientVer', '1.3.1'), ('loginPlatform', 'android'), ('loginToken', ''), ('sign', '4efb209fb3021e09f737f3dc4a6ddec4'), ('systemOs', 'vivo'), ('timeStamp', 1563434953)]