tuple to dict
-
g = (('a','1'),('a','2'),('b','1'),('b','2')) dict((y, x) for y, x in g) {'a': '2', 'b': '2'}
How do you get all the keys and meanings in dict?
-
If I understand correctly, you need a multimap analog from C+++. There's no such thing in the python, but You can just use the dict that keeps the lists:
import collections from collections import defaultdict
g = (('a','1'),('a','2'),('b','1'),('b','2'))
d = defaultdict(list)
[d[x].append(y) for (x, y) in g]
print d>>> defaultdict(<type 'list'>, {'a': ['1', '2'], 'b': ['1', '2']})