zoukankan      html  css  js  c++  java
  • "sorted()"中的"Key Functions"

    Pythonsorted()函数中可以加入key=<FUNCTION>参数。作用是每个元素在排序之前,先作为key=<FUNCTION>FUNCTION的参数,用FUNCTION的输出结果作为依据来排序。

    print (sorted("This is a test string from Dufresne".split(), key = str.lower))
    

    输出结果:

    ['a', 'Dufresne', 'from', 'is', 'string', 'test', 'This']
    
    students = [('alice', 'B', 12), ('eliza', 'A', 16), ('tae', 'C', 15)]
    print(sorted(students, key = lambda student: student[0]))
    

    这里定义了一个lambda: student: student[0]的函数。把一个list作为输入参数student,返回list中的第1个元素student[0]

  • 相关阅读:
    【Coreforces 1253E】
    计数专题乱做
    PKUWC2020乱做
    多项式板子
    notepad
    2021.4.9
    2021.4.8
    2021.3.31
    2021.3.26
    2021.3.25
  • 原文地址:https://www.cnblogs.com/yaos/p/7106036.html
Copyright © 2011-2022 走看看