Files
Mastering-Python-2e-code_2/CH_04_design_patterns/T_07_defaultdict.rst
T
2022-05-05 18:25:55 +02:00

94 lines
2.1 KiB
ReStructuredText

>>> nodes = [
... ('a', 'b'),
... ('a', 'c'),
... ('b', 'a'),
... ('b', 'd'),
... ('c', 'a'),
... ('d', 'a'),
... ('d', 'b'),
... ('d', 'c'),
... ]
------------------------------------------------------------------------------
>>> graph = dict()
>>> for from_, to in nodes:
... if from_ not in graph:
... graph[from_] = []
... graph[from_].append(to)
>>> import pprint
>>> pprint.pprint(graph)
{'a': ['b', 'c'],
'b': ['a', 'd'],
'c': ['a'],
'd': ['a', 'b', 'c']}
------------------------------------------------------------------------------
>>> import collections
>>> graph = collections.defaultdict(list)
>>> for from_, to in nodes:
... graph[from_].append(to)
>>> import pprint
>>> pprint.pprint(graph)
defaultdict(<class 'list'>,
{'a': ['b', 'c'],
'b': ['a', 'd'],
'c': ['a'],
'd': ['a', 'b', 'c']})
------------------------------------------------------------------------------
>>> counter = collections.defaultdict(int)
>>> counter['spam'] += 5
>>> counter
defaultdict(<class 'int'>, {'spam': 5})
------------------------------------------------------------------------------
>>> import collections
>>> def tree(): return collections.defaultdict(tree)
------------------------------------------------------------------------------
>>> import json
>>> import collections
>>> def tree():
... return collections.defaultdict(tree)
>>> colours = tree()
>>> colours['other']['black'] = 0x000000
>>> colours['other']['white'] = 0xFFFFFF
>>> colours['primary']['red'] = 0xFF0000
>>> colours['primary']['green'] = 0x00FF00
>>> colours['primary']['blue'] = 0x0000FF
>>> colours['secondary']['yellow'] = 0xFFFF00
>>> colours['secondary']['aqua'] = 0x00FFFF
>>> colours['secondary']['fuchsia'] = 0xFF00FF
>>> print(json.dumps(colours, sort_keys=True, indent=4))
{
"other": {
"black": 0,
"white": 16777215
},
"primary": {
"blue": 255,
"green": 65280,
"red": 16711680
},
"secondary": {
"aqua": 65535,
"fuchsia": 16711935,
"yellow": 16776960
}
}