@@ -98,19 +98,9 @@ class Solution:
98
98
- https://www.runoob.com/w3cnote/python-negative-storage.html
99
99
- https://leetcode-cn.com/problems/convert-integer-lcci/solution/python3-zhu-yi-qi-dui-yu-fu-shu-de-cun-chu-fang-sh/
100
100
101
- # python list/dict 排序等技巧
101
+ # python list 技巧
102
102
103
103
``` py
104
- # python 根据 key,value 排序字典
105
- d = {' d' : 4 , ' a' : 1 , ' b' : 2 , ' c' :3 }
106
- # dict sort by key and reverse
107
- dict (sorted (d.items())) # {'a': 1, 'b': 2, 'c': 3, 'd': 4}
108
- dict (sorted (d.items(), reverse = True )) # {'d': 4, 'c': 3, 'b': 2, 'a': 1}
109
-
110
- # dict sort by value and reverse
111
- dict (sorted (d.items(), key = lambda kv :kv[1 ])) # {'a': 1, 'b': 2, 'c': 3, 'd': 4}
112
- dict (sorted (d.items(), key = lambda kv :kv[1 ], reverse = True )) # {'d': 4, 'c': 3, 'b': 2, 'a': 1}
113
-
114
104
# 排序嵌套 list
115
105
l = [(' a' , 1 ), (' c' , 2 ), (' b' ,3 )]
116
106
sorted (l, key = lambda p :p[0 ]) # 根据第1个值排序,[('a', 1), ('b', 3), ('c', 2)]
@@ -120,18 +110,11 @@ sorted(l, key=lambda p:p[1]) # 根据第2个值排序,[('a', 1), ('c', 2), ('b
120
110
l = [1 ,2 ,5 ,4 ,3 ]
121
111
maxi, maxval = max (enumerate (l), key = lambda iv : iv[1 ]) # 2, 5
122
112
123
- # 获取字典对应的最大值对应的 key,value
124
- mydict = {' A' :4 ,' B' :10 ,' C' :0 ,' D' :87 }
125
- maximum = max (mydict, key = mydict.get) # Just use 'min' instead of 'max' for minimum.
126
- maxk, maxv = maximum, mydict[maximum]
127
- # 或者
128
- maxk, maxv = max (mydict.items(), key = lambda k : k[1 ])
129
-
130
- # python3 排序list自定义函数(python2 直接用 cmp 参数)
113
+ # python3 排序list自定义函数(python2 直接用 cmp 参数, python3 需要用 cmp_to_key 转成 key 参数)
131
114
from functools import cmp_to_key
132
115
nums = [3 ,2 ,1 ,4 ,5 ]
133
- sorted (nums, key = cmp_to_key(lambda a ,b : a- b) ) # [1 ,2 ,3, 4, 5]
134
- sorted (nums, key = cmp_to_key(lambda a ,b : b- a) ) # [5, 4, 3, 2, 1]
116
+ sorted (nums, key = cmp_to_key(lambda a ,b : a- b) ) # [1 ,2 ,3, 4, 5]
117
+ sorted (nums, key = cmp_to_key(lambda a ,b : b- a) ) # [5, 4, 3, 2, 1]
135
118
136
119
# 一行代码判断列表是否有序
137
120
issorted = all (l[i] <= l[i+ 1 ] for i in range (len (l) - 1 ))
@@ -140,10 +123,36 @@ issorted = all(l[i] <= l[i+1] for i in range(len(l) - 1))
140
123
from itertools import accumulate
141
124
presums = list (accumulate([1 ,2 ,3 ])) # [1, 3, 6]
142
125
143
- # 一行代码求矩阵和 https://stackoverflow.com/questions/10713150/how-to-sum-a-2d-array-in-python
126
+ # 一行代码求矩阵元素总和 https://stackoverflow.com/questions/10713150/how-to-sum-a-2d-array-in-python
144
127
allsum = sum (map (sum , matrix)) # 或者 allsum = sum((sum(row) for row in matrix))
145
128
```
146
129
130
+ # python dict 技巧
131
+
132
+ ``` py
133
+ # python 根据 key,value 排序字典
134
+ d = {' d' : 4 , ' a' : 1 , ' b' : 2 , ' c' :3 }
135
+ # dict sort by **key** and reverse
136
+ dict (sorted (d.items())) # {'a': 1, 'b': 2, 'c': 3, 'd': 4}
137
+ dict (sorted (d.items(), reverse = True )) # {'d': 4, 'c': 3, 'b': 2, 'a': 1}
138
+
139
+ # dict sort by **value** and reverse
140
+ dict (sorted (d.items(), key = lambda kv :kv[1 ])) # {'a': 1, 'b': 2, 'c': 3, 'd': 4}
141
+ dict (sorted (d.items(), key = lambda kv :kv[1 ], reverse = True )) # {'d': 4, 'c': 3, 'b': 2, 'a': 1}
142
+
143
+ # 获取字典对应的最大值对应的 key,value
144
+ mydict = {' A' :4 ,' B' :10 ,' C' :0 ,' D' :87 }
145
+ maximum = max (mydict, key = mydict.get) # Just use 'min' instead of 'max' for minimum.
146
+ maxk, maxv = maximum, mydict[maximum]
147
+ # 或者
148
+ maxk, maxv = max (mydict.items(), key = lambda k : k[1 ])
149
+
150
+ # 支持默认值的有序字典 (OrderedDict and defaultdict)
151
+ # https://stackoverflow.com/questions/6190331/how-to-implement-an-ordered-default-dict
152
+ od = OrderedDict() # collections.OrderedDict()
153
+ od[i] = od.get(i, 0 ) + 1 # 间接实现了 defaultdict(int) ,同时保持了插入字典的 key 顺序
154
+ ```
155
+
147
156
# 链表题目调试函数
148
157
149
158
``` py
0 commit comments