Arbitrarily nested dictionary from tuples

前端 未结 2 986
猫巷女王i
猫巷女王i 2020-12-19 18:21

Given a dictionary with tuples as keys (and numbers/scalars as values), what is a Pythonic way to convert to a nested dictionary? The hitch is that from input-to-input, the

2条回答
  •  醉话见心
    2020-12-19 19:11

    You can use itertools.groupby with recursion:

    from itertools import groupby
    def create_nested_dict(d):
      _c = [[a, [(c, d) for (_, *c), d in b]] for a, b in groupby(sorted(d, key=lambda x:x[0][0]), key=lambda x:x[0][0])]
      return {a:b[0][-1] if not any([c for c, _ in b]) else create_nested_dict(b) for a, b in _c}
    

    from itertools import product
    
    d1 = dict(zip(product('AB', [0, 1]), range(2*2)))
    d2 = dict(zip(product('AB', [0, 1], [True, False]), range(2*2*2)))
    d3 = dict(zip(product('CD', [0, 1], [True, False], 'AB'), range(2*2*2*2)))
    print(create_nested_dict(d1.items()))
    print(create_nested_dict(d2.items()))
    print(create_nested_dict(d3.items())) 
    

    Output:

    {'A': {0: 0, 1: 1}, 'B': {0: 2, 1: 3}}
    {'A': {0: {False: 1, True: 0}, 1: {False: 3, True: 2}}, 'B': {0: {False: 5, True: 4}, 1: {False: 7, True: 6}}}
    {'C': {0: {False: {'A': 2, 'B': 3}, True: {'A': 0, 'B': 1}}, 1: {False: {'A': 6, 'B': 7}, True: {'A': 4, 'B': 5}}}, 'D': {0: {False: {'A': 10, 'B': 11}, True: {'A': 8, 'B': 9}}, 1: {False: {'A': 14, 'B': 15}, True: {'A': 12, 'B': 13}}}}
    

提交回复
热议问题