Skip to content
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

provide example of list of dictionaries and filtering by intermediate key #42

Closed
binki opened this issue Sep 29, 2015 · 3 comments
Closed
Labels

Comments

@binki
Copy link

binki commented Sep 29, 2015

Say I have this:

d = {'x': [{'a': 5, 'b': 2}, {'a': 6, 'b': 4}]}

and I want to list all the values of a. I cannot get from the README that I would use:

dpath.util.values(d, '/x/*/a')

Also, how can I look up the value of 'b' for when 'a' is 6, for example? Is there a place to look for documentation (that could be linked from the README?) Am I not looking for docs in the right place just because I’m unfamiliar with python? ;-)

Thanks for any help or doc updates!

@xhh2a
Copy link
Contributor

xhh2a commented Sep 30, 2015

dpath:

>>> import dpath.util
>>> d = {'x': [{'a': 5, 'b': 2}, {'a': 6, 'b': 4}]}
>>> dpath.util.values(d, 'x')
[[{'a': 5, 'b': 2}, {'a': 6, 'b': 4}]]
>>> dpath.util.values(d, 'x/*')
[{'a': 5, 'b': 2}, {'a': 6, 'b': 4}]
>>> dpath.util.values(d, 'x/*/**/a')
[5, 6]

funcy:

>>> from funcy import get_in
>>> get_in(d, ["x"])
[{'a': 5, 'b': 2}, {'a': 6, 'b': 4}]
>>> from funcy import pluck
>>> pluck("a", get_in(d, ["x"]))
[5, 6]

@akesterson
Copy link
Collaborator

@xhh2a answered your first question nicely, so I'll focus on the second.

Also, how can I look up the value of 'b' for when 'a' is 6, for example?

This is not explicitly documented (not every use case is in the documentation). This is currently a 2 step process (though a more elegant solution is currently being discussed in #40 ).

>>> for (path, value) in dpath.util.search(d, '/x/*/a', afilter=(lambda x: x == 6), yielded=True):
...     parentpath = path.split('/')[:-1]
...     print dpath.util.search(d, '/'.join([parentpath, 'b']))
... 
{'x': [{'b': 4}]}

I'm going to make a master ticket for documentation updates and link this from there; closing for now.

@abarnert
Copy link

I think @binki was asking why this works:

>>> dpath.util.values(d, '/x/*/a')
[5, 6]

… rather than complaining that it doesn't work and asking what to do instead, which is what @xhh2a answered.

But it does work, and if you just had a single example showing a * glob pattern, it would be obvious why.

Right now, there is such an example, but it's wrong: the '/a/b/d/\*' won't match anything (see #80 for why it wouldn't even match a literal '*' key), and what you really want is /a/b/d/*. Although even then it's kind of pointless, because you'll get the same results from a/b/d. A better example might be showing multiple searches: a/*/d/0, a/b/*/0, a/**/0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants