Skip to content

Commit f0a961b

Browse files
committed
python3 sort list
1 parent b2c3b3a commit f0a961b

File tree

1 file changed

+6
-0
lines changed
  • docs/19_python内置常用算法和数据结构

1 file changed

+6
-0
lines changed

docs/19_python内置常用算法和数据结构/builtins.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ maximum = max(mydict, key=mydict.get) # Just use 'min' instead of 'max' for min
9090
maxk, maxv = maximum, mydict[maximum]
9191
# 或者
9292
maxk, maxv = max(mydict.items(), key=lambda k: k[1])
93+
94+
# python3 排序list自定义函数(python2 直接用 cmp 参数)
95+
from functools import cmp_to_key
96+
nums = [3,2,1,4,5]
97+
sorted(nums, key= cmp_to_key(lambda a,b: a-b) ) # [1 ,2 ,3, 4, 5]
98+
sorted(nums, key= cmp_to_key(lambda a,b: b-a) ) # [5, 4, 3, 2, 1]
9399
```
94100

95101
# 链表题目调试函数

0 commit comments

Comments
 (0)