ch02
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
# defaultdict.py
|
||||
|
||||
|
||||
>>> d = {}
|
||||
>>> d['age'] = d.get('age', 0) + 1 # age not there, we get 0 + 1
|
||||
>>> d
|
||||
{'age': 1}
|
||||
>>> d = {'age': 39}
|
||||
>>> d['age'] = d.get('age', 0) + 1 # age is there, we get 40
|
||||
>>> d
|
||||
{'age': 40}
|
||||
|
||||
|
||||
>>> from collections import defaultdict
|
||||
>>> dd = defaultdict(int) # int is the default type (0 the value)
|
||||
>>> dd['age'] += 1 # short for dd['age'] = dd['age'] + 1
|
||||
>>> dd
|
||||
defaultdict(<class 'int'>, {'age': 1}) # 1, as expected
|
||||
Reference in New Issue
Block a user