|
28 | 28 |
|
29 | 29 | 如果你使用 python2 or python3 刷题(比如力扣leetcode),有一些坑或者技巧需要注意:
|
30 | 30 |
|
31 |
| -- python3 和 python2 的 dict 有所用不同,python3.7 之后的 dict 会保持插入顺序, python2 不要依赖 dict 迭代顺序,请使用 OrderedDict |
| 31 | +- python3 和 python2 的 dict 有所用不同,python3.7 之后的 dict 会保持插入顺序(不是字典序), python2 不要依赖 dict 迭代顺序,请使用 OrderedDict |
32 | 32 | - 正确初始化一个二维数组:`dp = [[0 for _ in range(col)] for _ in range(row)]`,不要用 `dp = [[0] * n] * m`, 否则里边都
|
33 |
| -引用的同一个 list,修改一个都会变 |
| 33 | +引用的同一个 list,修改一个都会变。`[[val]*cols for _ in range(rows)]` 也行 |
34 | 34 | - python在数值范围建议用:`MAXINT = 2**63-1; MININT = -2**63` 。因为 python2 sys.maxint 和 python3 sys.maxsize 不统一
|
35 | 35 | - 优先级队列:使用内置queue.PriorityQueue or heapq ,定义一个 Item 类实现"小于" 魔术方法就可以实现,下边有代码演示
|
36 | 36 | - python3 的 functools 模块自带了 cache(等价于lru_cache(maxsize=None)) 和 lru_cache 装饰器,在一些需要递归记忆化搜索的时候会很方便
|
@@ -59,18 +59,27 @@ MAXINT = (1<<63) - 1
|
59 | 59 | MININT = ~MAXINT
|
60 | 60 | ```
|
61 | 61 |
|
62 |
| -# python dict 排序 |
| 62 | +# python list/dict 排序技巧 |
63 | 63 |
|
64 | 64 | ```py
|
65 |
| -# 补充 python 根据 key,value 排序字典的 |
| 65 | +# python 根据 key,value 排序字典 |
66 | 66 | d = {'d': 4, 'a': 1, 'b': 2, 'c':3}
|
67 |
| -# sort by key and reverse |
| 67 | +# dict sort by key and reverse |
68 | 68 | dict(sorted(d.items())) # {'a': 1, 'b': 2, 'c': 3, 'd': 4}
|
69 | 69 | dict(sorted(d.items(), reverse=True)) # {'d': 4, 'c': 3, 'b': 2, 'a': 1}
|
70 | 70 |
|
71 |
| -# sort by value and reverse |
| 71 | +# dict sort by value and reverse |
72 | 72 | dict(sorted(d.items(), key = lambda kv:kv[1])) # {'a': 1, 'b': 2, 'c': 3, 'd': 4}
|
73 | 73 | dict(sorted(d.items(), key = lambda kv:kv[1], reverse=True)) # {'d': 4, 'c': 3, 'b': 2, 'a': 1}
|
| 74 | + |
| 75 | +# 排序嵌套 list |
| 76 | +l = [('a', 1), ('c', 2), ('b',3)] |
| 77 | +sorted(l, key=lambda p:p[0]) # 根据第1个值排序,[('a', 1), ('b', 3), ('c', 2)] |
| 78 | +sorted(l, key=lambda p:p[1]) # 根据第2个值排序,[('a', 1), ('c', 2), ('b', 3)] |
| 79 | + |
| 80 | +# 同时获取最大值的下标和值 |
| 81 | +l = [1,2,5,4,3] |
| 82 | +maxi, maxval = max(enumerate(l), key=lambda iv: iv[1]) # 2, 5 |
74 | 83 | ```
|
75 | 84 |
|
76 | 85 | # 链表题目调试函数
|
@@ -173,11 +182,11 @@ class Item:
|
173 | 182 | def __lt__(self, other): # heapq 源码实现只用了 小于 比较,这里定义了就可以 push 一个 item 类
|
174 | 183 | return self.weight < other.weight
|
175 | 184 |
|
176 |
| - def __eq__(self, other): # 这个可以省略,只要定义了 __lt__ 魔法函数就可以了 |
177 |
| - return self.weight == other.weight |
178 |
| - |
179 |
| - def __str__(self): |
180 |
| - return '{}:{}'.format(self.key,self.weight) |
| 185 | +# def __eq__(self, other): # 这个可以省略,只要定义了 __lt__ 魔法函数就可以了 |
| 186 | +# return self.weight == other.weight |
| 187 | +# |
| 188 | +# def __str__(self): |
| 189 | +# return '{}:{}'.format(self.key,self.weight) |
181 | 190 |
|
182 | 191 | # Item.__lt__ = lambda self, other: self.weight < other.weight # 对于已有的类,直接加一句就可以实现作为 heap item 了
|
183 | 192 |
|
@@ -426,7 +435,7 @@ class Solution(object):
|
426 | 435 | return n+1
|
427 | 436 | ```
|
428 | 437 |
|
429 |
| -# 兼容代码提交格式 |
| 438 | +# 兼容代码ACM/核心提交格式 |
430 | 439 |
|
431 | 440 | 注意牛客网有两种模式,一种是和 leetcode 一样的提交(无需处理输入),只需要提交核心代码。
|
432 | 441 | 一种是 ACM 模式,还需要自己处理输入和输出。
|
|
0 commit comments