-
Notifications
You must be signed in to change notification settings - Fork 1
/
dicttools.py
39 lines (33 loc) · 1.21 KB
/
dicttools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
_SORTED = sorted
class Grouper(dict):
def plus(self, key, value_element):
try:
extant = self[key]
except KeyError:
self[key] = extant = []
extant.append(value_element)
def plusdate(self, key_value_pairs):
for key, value in key_value_pairs:
try:
extant = self[key]
except KeyError:
self[key] = extant = []
extant.append(value)
def invert_mapping(mapping, single=False):
"""Map from the values of `mapping` to their associated keys.
:param mapping: a dict whose values are hashable or containers of hashables.
:param single: True if values are to be keys in returned dict.
:returns: a dict mapping from the values (or values' elements) to collection of keys.
"""
result = {}
for k,V in mapping.items():
iterable_V = [V] if single else V
for v in iterable_V:
try:
extant = result[v]
except KeyError:
result[v] = extant = set()
extant.add(k)
return result
def sorted(dict):
return {key:dict[key] for key in _SORTED(dict)}