unflatten with lists
See original GitHub issueFlattening a nested dict that contains lists works great, but unflatten makes dicts instead of lists when index is list index. I rewrote part of your lib to unflatten for my needs and thought you might want to integrate it into you unflatten.
I’m worried that my changes aren’t generic enough work for all kinds of mixed list with dict.
Here is I how did the unflattening. The only function I change is this one:
def nested_set_dict(d, keys, value):
"""Set a value to a sequence of nested keys
Parameters
----------
d : Mapping
keys : Sequence[str]
value : Any
"""
assert keys
key = keys[0]
if len(keys) == 1:
if type(d) == list:
d.append(value)
else:
d[key] = value
return
# the type is a string so make a dict if none exists
if type(keys[1]) == int:
if key in d:
pass
else:
d[key] = []
d = d[key]
elif type(key)==int:
if (key+1) > len(d):
d.append({})
d = d[key]
else:
d = d.setdefault(key, {})
nested_set_dict(d, keys[1:], value)
Testing it out:
d1 = {'a':{'b':[{'c1':'nested1!','d1':[{'e1':'so_nested1!!!'}]},
{'c2':'nested2!','d2':[{'e2':'so_nested2!!!'}]},
{'c3':'nested3!','d3':[{'e3':'so_nested3!!!'}]},
{'c4':'nested4!','d4':[{'e4':'so_nested4a!!!'},
{'e4':'so_nested4b!!!'},
{'e4':'so_nested4c!!!'},
{'e4':'so_nested4d!!!'},
{'e4':'so_nested4e!!!'}]}]}}
Flatten works great for this out of the box
df = mzm.flatten(d1,enumerate_types=(list,))
kv = sorted([(k,v) for (k,v) in df.items()])
((‘a’, ‘b’, 0, ‘c1’), ‘nested1!’) ((‘a’, ‘b’, 0, ‘d1’, 0, ‘e1’), ‘so_nested1!!!’) ((‘a’, ‘b’, 1, ‘c2’), ‘nested2!’) ((‘a’, ‘b’, 1, ‘d2’, 0, ‘e2’), ‘so_nested2!!!’) ((‘a’, ‘b’, 2, ‘c3’), ‘nested3!’) ((‘a’, ‘b’, 2, ‘d3’, 0, ‘e3’), ‘so_nested3!!!’) ((‘a’, ‘b’, 3, ‘c4’), ‘nested4!’) ((‘a’, ‘b’, 3, ‘d4’, 0, ‘e4’), ‘so_nested4a!!!’) ((‘a’, ‘b’, 3, ‘d4’, 1, ‘e4’), ‘so_nested4b!!!’) ((‘a’, ‘b’, 3, ‘d4’, 2, ‘e4’), ‘so_nested4c!!!’) ((‘a’, ‘b’, 3, ‘d4’, 3, ‘e4’), ‘so_nested4d!!!’) ((‘a’, ‘b’, 3, ‘d4’, 4, ‘e4’), ‘so_nested4e!!!’)
d2 = {}
for key_value in kv:
k = key_value[0]
v = key_value[1]
nested_set_dict(d2,k,v)
Gives
d1 =
{'a': {'b': [{'c1': 'nested1!', 'd1': [{'e1': 'so_nested1!!!'}]}, {'c2': 'nested2!', 'd2': [{'e2': 'so_nested2!!!'}]}, {'c3': 'nested3!', 'd3': [{'e3': 'so_nested3!!!'}]}, {'d4': [{'e4': 'so_nested4a!!!'}, {'e4': 'so_nested4b!!!'}, {'e4': 'so_nested4c!!!'}, {'e4': 'so_nested4d!!!'}, {'e4': 'so_nested4e!!!'}], 'c4': 'nested4!'}]}}
d2 =
{'a': {'b': [{'c1': 'nested1!', 'd1': [{'e1': 'so_nested1!!!'}]}, {'c2': 'nested2!', 'd2': [{'e2': 'so_nested2!!!'}]}, {'c3': 'nested3!', 'd3': [{'e3': 'so_nested3!!!'}]}, {'d4': [{'e4': 'so_nested4a!!!'}, {'e4': 'so_nested4b!!!'}, {'e4': 'so_nested4c!!!'}, {'e4': 'so_nested4d!!!'}, {'e4': 'so_nested4e!!!'}], 'c4': 'nested4!'}]}}
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:15 (5 by maintainers)
Top GitHub Comments
@ianlini & @ori-levi Hello, are there any updates to this planed feature?
@ianlini I just finish do develop the suggested solution, with JSONPath. Note that only JSONPath is reverseable.
I reformatted my code and write some test and open pull request for this. I hope to do this before Sunday.