Skip to content

Commit 8fc6d7d

Browse files
committed
python list and dict skills
1 parent 433680b commit 8fc6d7d

File tree

1 file changed

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

1 file changed

+31
-22
lines changed

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

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,9 @@ class Solution:
9898
- https://www.runoob.com/w3cnote/python-negative-storage.html
9999
- https://leetcode-cn.com/problems/convert-integer-lcci/solution/python3-zhu-yi-qi-dui-yu-fu-shu-de-cun-chu-fang-sh/
100100

101-
# python list/dict 排序等技巧
101+
# python list 技巧
102102

103103
```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-
114104
# 排序嵌套 list
115105
l = [('a', 1), ('c', 2), ('b',3)]
116106
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
120110
l = [1,2,5,4,3]
121111
maxi, maxval = max(enumerate(l), key=lambda iv: iv[1]) # 2, 5
122112

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 参数)
131114
from functools import cmp_to_key
132115
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]
135118

136119
# 一行代码判断列表是否有序
137120
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))
140123
from itertools import accumulate
141124
presums = list(accumulate([1,2,3])) # [1, 3, 6]
142125

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
144127
allsum = sum(map(sum, matrix)) # 或者 allsum = sum((sum(row) for row in matrix))
145128
```
146129

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+
147156
# 链表题目调试函数
148157

149158
```py

0 commit comments

Comments
 (0)