課程
/后端開發
/Python
/python進階
為什么不能寫成 這樣呢?
L2?=?sorted(L1,?key?=?lambda?p1,?p2:?cmp(p1.name,?p2.name))
多了個key就錯了, key到底有什么用啊
2015-07-20
源自:python進階 4-3
正在回答
In Python “key” is the function that process elements and get the value for the comparison.
?
key只接受一個元素
>>>?help(sorted) Help?on?built-in?function?sorted?in?module?__builtin__: sorted(...) ????sorted(iterable,?cmp=None,?key=None,?reverse=False)?-->?new?sorted?list
iterable:是可迭代類型;cmp:用于比較的函數,比較什么由key決定,有默認值,迭代集合中的一項;key:用列表元素的某個屬性和函數進行作為關鍵字,有默認值,迭代集合中的一項;reverse:排序規則. reverse = True 或者 reverse = False,有默認值。返回值:是一個經過排序的可迭代類型,與iterable一樣。?一般來說,cmp和key可以使用lambda表達式。
舉報
學習函數式、模塊和面向對象編程,掌握Python高級程序設計
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2016-06-22
In Python “key” is the function that process elements and get the value for the comparison.
?
2015-11-24
key只接受一個元素
2015-10-12
iterable:是可迭代類型;
cmp:用于比較的函數,比較什么由key決定,有默認值,迭代集合中的一項;
key:用列表元素的某個屬性和函數進行作為關鍵字,有默認值,迭代集合中的一項;
reverse:排序規則. reverse = True 或者 reverse = False,有默認值。
返回值:是一個經過排序的可迭代類型,與iterable一樣。
?
一般來說,cmp和key可以使用lambda表達式。