Last active
June 15, 2017 07:01
-
-
Save askeing/d80137f8288d64a9bdbd9832c1b335c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> # Soultion 1 | |
... | |
>>> data = { | |
... "id-1": { | |
... "p": "w", | |
... "t": "foo", | |
... "b": "f" | |
... }, | |
... "id-2": { | |
... "p": "w", | |
... "t": "bar", | |
... "b": "c" | |
... }, | |
... "id-3": { | |
... "p": "m", | |
... "t": "bar", | |
... "b": "f" | |
... } | |
... } | |
>>> | |
>>> [k for k, v in data.items() if v.get('p') == 'w'] | |
['id-1', 'id-2'] | |
>>> [k for k, v in data.items() if v.get('p') == 'm'] | |
['id-3'] | |
>>> [k for k, v in data.items() if v.get('t') == 'foo'] | |
['id-1'] | |
>>> [k for k, v in data.items() if v.get('p') == 'w' and v.get('b') == 'f'] | |
['id-1'] | |
>>> | |
>>> | |
>>> # Soultion 2 | |
... | |
>>> | |
>>> list_data = { | |
... "id-1": ["w", "foo", "f"], | |
... "id-2": ["w", "bar", "c"], | |
... "id-3": ["m", "bar", "f"] | |
... } | |
>>> [k for k, v in list_data.items() if 'w' in v] | |
['id-1', 'id-2'] | |
>>> [k for k, v in list_data.items() if 'w' in v and 'bar' in v] | |
['id-2'] | |
>>> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment