zoukankan      html  css  js  c++  java
  • python sorted()函数

    # -*- 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)]

  • 相关阅读:
    POJ 2029 (二维树状数组)题解
    HDU 4819 Mosaic (二维线段树&区间最值)题解
    HDU 1823 Luck and Love (二维线段树&区间最值)题解
    POJ1061 青蛙的约会(扩展欧几里得)题解
    POJ 2155 Matrix (二维树状数组)题解
    POJ 1751 Highways(最小生成树&Prim)题解
    HDU 6148 Valley Numer (数位DP)题解
    HDU3652 B-number(数位DP)题解
    HDU 4734 (数位DP)题解
    HDU 2089 不要62 (数位DP)题解
  • 原文地址:https://www.cnblogs.com/tallme/p/11239168.html
Copyright © 2011-2022 走看看