Closed therve closed 9 months ago
Ah, the grandparent key, sure! This isn't as pretty as I would like, but here's the working spec:
(
T.items(),
Iter(
(A.globals.gp_item, T[1].values(),
Iter({'val': 'c',
'path': S.globals.gp_item[0]}))
).flatten().all()
)
And I get:
[
{
"val": 1,
"path": "a"
}
]
Here's a link to a glompad where you can fool around with it in your browser.
Two key things to note:
globals
member of the scope (and the A
assignment shortcut) to save the grandparent item before we descend. Probably when you used S
the value was being lost because the scope was local.
Vars
documented in the link above.gp_item[0]
).Iter
specs. glom doesn't yet have full tree traversal, so the nesting of the spec is going to roughly match the nesting of your data. If you need this to work in an arbitrary nested structure, I recommend remap
, the recursive map function, part of boltons.iterutils
. Hope this helps!
Thanks a lot, I almost reached that one myself! For some reason I was focused on trying to save just the key T[0]
instead of the whole items
structure, this works.
And a related thank you for that great library.
Yeah, I know what you mean re: keys vs items. I might add this example to the docs, in case it's instructive for others too :)
Hi,
I'm struggling to do what seems to be a simple thing when reformatting data, basically store the grandparent dict key when iterating. A simple example:
gives me
[{'value': 1}]
as I expect. What I'd like is to have[{'value': 1, 'path': 'a'}]
. Any idea how to to achieve this? I tried to useS
but I never seems to get the scope right. It seems I'm missing something obvious. Thanks!