This commit is contained in:
adii1823
2021-10-28 17:33:49 +05:30
parent ff85709895
commit 583ec6a898
17 changed files with 952 additions and 0 deletions
+18
View File
@@ -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