Skip to content

Commit 2c523f7

Browse files
committed
python3 sort functools.cmp_to_key
1 parent fc05a51 commit 2c523f7

File tree

1 file changed

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

1 file changed

+2
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
- python3 的 functools 模块自带了 cache(等价于lru_cache(maxsize=None)) 和 lru_cache 装饰器,在一些需要递归记忆化搜索的时候会很方便
3737
- 除法变更:python2和 python3 除法做了变更要注意。还有负数除法。 python2 `int(6/-123)==-1`,但是 python3 `int(6/-123)==0`
3838
整数除法统一用"//"。比如二分求中间值 `mid=(l+r)//2` 或者 `mid=l+(r-l)//2`,因为python天生支持大数不会溢出两种写法都行
39+
- 自定义排序函数。python2 可以用 `nums.sort(cmp=lambda a, b: a - b)`,但是python3移除了cmp参数。
40+
python3如果想要用自定义排序函数可以使用 functools.cmp_to_key 函数改成 `nums.sort(key=cmp_to_key(lambda a, b: a - b))`
3941

4042
# python int 值范围
4143

0 commit comments

Comments
 (0)