首页 > 编程语言 > 详细

python, questions

时间:2020-01-18 09:54:31      阅读:67      评论:0      收藏:0      [点我收藏+]

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

原文:https://www.cnblogs.com/sarah-zhang/p/12207997.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!