看板 Python 關於我們 聯絡資訊
https://stackoverflow.com/questions/10645986/custom-sort-python 我想將 key = lambda word: [alphabet.index(c) for c in word[0]] 這行轉換成老式cmp function def mycmp(a, b): return a - b sorted(words, key = cmp_to_key(mycmp)) 但是不太會寫 請大家提示一下 a, b分別該是什麼? 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 8.41.187.50 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1519087618.A.4E7.html
djshen: 你要sort什麼 02/20 12:09
vfgce: 你用那一版的python,3版都是key,2版才用cmp. 02/20 12:30
vfgce: 然後,key一般接受一個parameter,cmp才有兩個parameter. 02/20 12:30
vfgce: 一般用key 就 02/20 12:32
vfgce: 是將傳入的parameter轉成可進行比較的object如number. 02/20 12:33
vfgce: 就可以進行比較了,不必像cmp接受兩個parameter還要自行定義 02/20 12:34
vfgce: 比較規則.... 02/20 12:34
vfgce: 你要進行反向比較,將sorted的reverse設成true也就可以.... 02/20 12:36
ar54971: https://goo.gl/2WCUGr 03/06 03:40
galeondx: https://goo.gl/cybm9m 03/06 04:18
galeondx: https://goo.gl/MKaCK6 03/06 04:31
galeondx: https://tinyurl.com/yadsk3lo 03/06 04:42