-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
unflatten with lists #8
Comments
Thanks for the advice. |
unflatten does not support lists, the github user benbowen created an issue: ianlini#8 and described a solution that worked for him.
unflatten does not support lists, the github user benbowen created an issue: ianlini#8 and described a solution that worked for him.
This would be a useful feature to me as well. It seems natural that the flatten/unflatten process should produce the same output as input, but that is not the case now. |
Sorry for reviving the issue, but any updates about it? Because I'm also flattering a dictionary that contains arrays, but unflattering it results with list indices that converted to dictionary keys. |
Because there is no further feedback about the design, and it seems to be the most requested feature, I will implement it according to my last comment. Not sure about the timing, maybe in 1 or 2 months. I would like to emphasize this again: I'm not expecting |
If one of the keys in the path just contains a number, then it can take as a list and insert the object in the specified index (in this case Then the only choice will be making these Sorry, I'm not experienced well in nested dictionaries and recursive stuff, because I know too how it is hard to deal with them, so I can only say these. |
@ysfchn , thanks for your suggestion. I have considered making the key as a special object. It is one of the most feasible idea in my mind. It's great to see that you have similar idea. I have also considered making Anyway, one of the difficulties is that I don't really know how people use this library. I guess people use it very differently, and I actually only use this library in some simple way. For example, If we make the key as a special object, then |
Hi! I'm investigating switching to using this project from another dict flattening library and this is one of the missing features that I would need. I was able to easily switch this out using the code that @benbowen provided, but it looks like 2 other use cases are missing:
Example to unflatten:
Example to flatten:
|
I just saw this in my email with the at-mention. I'm so sorry for 1 year of silence! but I'm excited that others are looking and working on this. The flatten/unflatten is an essential step in a pipeline that I have to run. I'm transferring the "battle code" I wrote for this pipeline about a year ago to another developer and by then if not sooner we will check out your commits and others suggestions. |
@benbowen I'm planning to implement this next week. |
If I can propose using the Ellipsis (python2.7) or ... (python3+) variable in place of array indexes.. that would indicate (at least with tuples formatting) that this is part of a list. {'roles': [
{'uuid': {'$uuid': '55e119ce-3b4f-11eb-adc7-00163e0987ed'}},
{'uuid': {'$uuid': '55e11cee-3b4f-11eb-adc7-00163e0987ed'}}
]} [(('roles', ..., 'uuid', '$uuid'), '55e119ce-3b4f-11eb-adc7-00163e0987ed'),
(('roles', ..., 'uuid', '$uuid'), '55e11cee-3b4f-11eb-adc7-00163e0987ed')] Or just use a type:
|
Hey! Thanks for creating this library 🥇 Would like to 1 up this issue as well. I'm looking into using this for a project which could benefit from dict flattening / unflattening - but unfortunately I would need this feature (i.e. given a dictionary which contains arrays in one or more values - flattening then unflattening results should result in arrays not being converted to dictionaries). |
Hey @ianlini, You can represent the dict key split by any delimiter, but when it's come to lists, append to the key the index.
flatten_dict
What do you think of this solution? I might implement this later this day and open a pull request to your library. |
Thanks @ori-levi, This might be a good starting point. I tried to implement a general version for all kinds of splitters a few months ago, but I found that there are so many edge cases and different behaviors to decide. The edge cases make the behavior less intuitive and less general no matter how I design it. After thinking a lot of those cases, I had a concrete idea on the requirements, but I became very busy before finishing the implementation. I knew JsonPath long time ago and use it a lot, but I seldom use flatten-dict. I am actually very curious about why people don't simply use JsonPath to access their dict if they only want to use a string as key to access it, so I didn't think in that direction. Anyway, I will be very happy if we can first have a reducer and splitter pair that can flatten a dict into your JsonPath format and unflatten it back. I will dig into making it more general or customizable in the future. |
@ianlini I just finish do develop the suggested solution, with JSONPath. I reformatted my code and write some test and open pull request for this. |
Is there a fork, which does it? |
Solution: flatten_json |
Flattening 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:
Testing it out:
Flatten works great for this out of the box
(('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!!!')
Gives
d1 =
d2 =
The text was updated successfully, but these errors were encountered: