zoukankan      html  css  js  c++  java
  • python, questions

    1. the list is a class which provides methods like sort(), append(), ...

    list.sort(*key=Nonereverse=None)

    key specifies a function of one argument that is used to extract a comparison key from each list element (for example, key=str.lower). The key corresponding to each item in the list is calculated once and then used for the entire sorting process. The default value of None means that list items are sorted directly without calculating a separate key value.

    reverse is a boolean value. If set to True, then the list elements are sorted as if each comparison were reversed.

    2. sorted() is a built-in function.  sorted(iterable*key=Nonereverse=False)

    1. Why use lambda expression?

    See the following code: 

    def key(x):

        return x[1]

    a = [(1,2),(3,1),(5,10),(11,-3)]

    a.sort(key = key)    # a = [(11,-3),(3,1),(1,2), (5,10)]

    a = [(1,2),(3,1),(5,10),(11,-3)]

    a.sort( key = lambda x : x[1] )

    #explanation: 

  • 相关阅读:
    北科的秋天
    最大子段和问题(dp)
    cmd应用
    问题 H: 抽奖活动(大数)
    大数算法
    模板整理(三)
    在CMD中建立一个不能删除的文件
    波利亚(Polya)罐子模型
    51nod-迷宫问题(Dijkstra算法)
    优先队列
  • 原文地址:https://www.cnblogs.com/sarah-zhang/p/12207997.html
Copyright © 2011-2022 走看看