Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert 2-tuple key in dictionary and add to a new dictionary as a dictionary value

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}}
like image 987
Jonathan C Avatar asked Apr 12 '18 04:04

Jonathan C


4 Answers

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
like image 131
Willem Van Onsem Avatar answered Sep 28 '22 07:09

Willem Van Onsem


A loop and dict.setdefault() can do that like:

Code:

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)

Results:

{0: {0: 0, 1: 1}, 1: {0: 1, 1: 0}}
like image 35
Stephen Rauch Avatar answered Sep 28 '22 06:09

Stephen Rauch


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}})
>>> 
like image 39
juanpa.arrivillaga Avatar answered Sep 28 '22 06:09

juanpa.arrivillaga


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
like image 35
wim Avatar answered Sep 28 '22 07:09

wim