Skip to content

Commit fb12fa0

Browse files
committed
add dict default
Change-Id: Icca1eff9aed99f0d6029da16006bed82bdea8ef7
1 parent fe5cafc commit fb12fa0

File tree

2 files changed

+55
-11
lines changed

2 files changed

+55
-11
lines changed

code/dict_default.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from collections import defaultdict, Counter
4+
5+
6+
if __name__ == '__main__':
7+
default_list = defaultdict(list)
8+
default_list["a"].append(1)
9+
default_list["a"].append(2)
10+
default_list["a"].append(3)
11+
print(default_list)
12+
13+
default_zero = Counter()
14+
default_zero["b"] += 1
15+
default_zero["b"] += 2
16+
default_zero["b"] += 3
17+
print(default_zero)

content/collections.md

+38-11
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ collections 一个重要的 类 就是计数器,妈妈再也不用担心我不
66

77
小时候我们数数是这样数的
88

9-
```
9+
```python
1010
# coding=utf-8
1111

1212
words = ['a', 'b', 'c', 'b', 'a', 'a', 'b', 'c']
@@ -25,7 +25,7 @@ print cnt
2525

2626
现在我们可这样数数
2727

28-
```
28+
```python
2929
# coding=utf-8
3030

3131
words = ['a', 'b', 'c', 'b', 'a', 'a', 'b', 'c']
@@ -43,7 +43,7 @@ print cnt.most_common(2)
4343

4444
其实使用原生列表也是可以的,像这样
4545

46-
```
46+
```python
4747
# coding=utf-8
4848

4949
words = ['a', 'b', 'c', 'b', 'a', 'a', 'b', 'c']
@@ -59,7 +59,7 @@ print count
5959

6060
还可以使用其进行统计文件
6161

62-
```
62+
```python
6363
# coding=utf-8
6464

6565

@@ -71,13 +71,40 @@ print(line_count)
7171

7272
```
7373

74+
2021-03-28
75+
76+
一般在获取默认值的时候都比较烦,需要使用 `.get` 去获取。
77+
78+
在对于默认是列表的字典,我们常用 `defaultdict` 去创建,那么对于默认为零的字典,就可以使用 `Counter` 去创建
79+
80+
```python
81+
# -*- coding: utf-8 -*-
82+
83+
from collections import defaultdict, Counter
84+
85+
86+
if __name__ == '__main__':
87+
default_list = defaultdict(list)
88+
default_list["a"].append(1)
89+
default_list["a"].append(2)
90+
default_list["a"].append(3)
91+
print(default_list)
92+
93+
default_zero = Counter()
94+
default_zero["b"] += 1
95+
default_zero["b"] += 2
96+
default_zero["b"] += 3
97+
print(default_zero)
98+
99+
```
100+
74101
### 字典
75102

76103
一般的字典进行赋值操作时,需要先检查键是否存在,`defaultdict` 则不需要
77104

78105
> 使用 defaultdict 的时候需注意,初始化的时候需要填入想要的值的类型。
79106
80-
```
107+
```python
81108
# coding=utf-8
82109

83110
from collections import defaultdict
@@ -111,7 +138,7 @@ some_dict['colours']['favourite'] = "yellow"
111138

112139
直接进行嵌套赋值
113140

114-
```
141+
```python
115142
import collections
116143
tree = lambda: collections.defaultdict(tree)
117144
some_dict = tree()
@@ -122,7 +149,7 @@ some_dict['colours']['favourite'] = "yellow"
122149

123150
在 Python 内置函数库 Queue 中已经提供了队列和栈, 在这里提供了双端队列 deque ,可以两边插入,两边取出。
124151

125-
```
152+
```python
126153
# coding=utf-8
127154

128155
from collections import deque
@@ -156,7 +183,7 @@ deque([4, 3, 1, 2])
156183

157184
还可以限制队列长度,超出长度的数据会被另一端挤出, 还可以进行队列的循环移位,左移或右移
158185

159-
```
186+
```python
160187
# coding=utf-8
161188

162189
from collections import deque
@@ -195,7 +222,7 @@ deque([6, 3, 2, 1, 0], maxlen=5)
195222

196223
字典是无序的,输入与输出的顺序都不一定一样, collections 中的 OrderedDict 能保证输出输入顺序绝对一致
197224

198-
```
225+
```python
199226
# coding=utf-8
200227

201228
from collections import OrderedDict
@@ -223,7 +250,7 @@ print o
223250

224251
namedtuple 是一个字典类型的元组,同样是不可变的,但是它又和字典一样存在键值对。
225252

226-
```
253+
```python
227254
# coding=utf-8
228255

229256
from collections import namedtuple
@@ -249,7 +276,7 @@ print perry._asdict
249276

250277
但是在 collections 库中,只有在 Python 3.4 及以上才能够使用 Enum 枚举类型
251278

252-
```
279+
```python
253280
# coding=utf-8
254281

255282
from collections import namedtuple

0 commit comments

Comments
 (0)