So I have the values:
values = {(0, 0): 0, (0, 1): 1, (1, 0): 1, (1, 1): 0}
and I want to convert the above dictionary to be:
values = {0: {0: 0, 1: 1}, 1: {0: 1, 1: 0}}
my function:
def convert(values : {(int,int): int}) -> {int: {int: int}}:
dictionary = {}
l = []
for k in d.keys():
l.append(k)
for k,v in d.items():
for i in l:
if i == k:
dictionary[v] = dict(l)
return dictionary
but I'm getting this as my output instead:
values = {0: {0: 1, 1: 1}, 1: {0: 1, 1: 1}}
A solution for an arbitrary depth:
def convert_tupledict(d):
result = {}
for ks, v in d.items():
subresult = result
*kis, ks = ks
for ki in kis:
if ki in subresult:
subresult = subresult[ki]
else:
subresult[ki] = subresult = {}
subresult[ks] = v
return result
We can then call it with:
convert_tupledict({(0, 0): 0, (0, 1): 1, (1, 0): 1, (1, 1): 0})
For 2-tuples, the following sulution should be sufficient:
def convert_2tupledict(d):
result = {}
for (k1, k2), v in d.items():
result.setdefault(k1, {})[k2] = v
return result
A loop and dict.setdefault()
can do that like:
values = {(0, 0): 0, (0, 1): 1, (1, 0): 1, (1, 1): 0}
result = {}
for k, v in values.items():
result.setdefault(k[0], {})[k[1]] = v
print(result)
{0: {0: 0, 1: 1}, 1: {0: 1, 1: 0}}
You just want to group things. The idiomatic way is to use a defaultdict
:
>>> from collections import defaultdict
>>> values = {(0, 0): 0, (0, 1): 1, (1, 0): 1, (1, 1): 0}
>>> new_values = defaultdict(dict)
>>> for (x,y), v in values.items():
... new_values[x][y] = v
...
>>> new_values
defaultdict(<class 'dict'>, {0: {0: 0, 1: 1}, 1: {0: 1, 1: 0}})
>>>
I propose for you a more general approach:
from collections import defaultdict
def tree():
def the_tree():
return defaultdict(the_tree)
return the_tree()
t = tree()
for (x, y), z in values.items():
t[x][y] = z
To "close" any node of the tree from further additions, just set its default factory to None
. For example, to seal it at the trunk:
>>> t.default_factory = None
>>> t[2]
# KeyError
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With