19 lines
444 B
Python
19 lines
444 B
Python
# 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
|