python, questions

Posted 心中有阳光

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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: 

以上是关于python, questions的主要内容,如果未能解决你的问题,请参考以下文章

python 有用的Python代码片段

Python 向 Postman 请求代码片段

python [代码片段]一些有趣的代码#sort

使用 Python 代码片段编写 LaTeX 文档

python 机器学习有用的代码片段

python 代码片段和解决方案