Grab Values Within a Dictionary in a Dictionary

jetmas

I may have not written the best of codes, but let's say I have a dictionary within a dictionary which output looks like this:

print node
> {
>  0: {'NODE': 'CLOUDY', 'GATE': 'EQUAL', 'PROB': 0.5}, 
>  1: {'NODE': 'RAIN', 'CPT_1': 0.2, 'CPT_0': 0.8, 'GATE': 'OR', 'PROB': 0.5, 'PNODE_0': 'Cloudy'}, 
>  2: {'NODE': 'SPRINKLER', 'CPT_1': 0.9, 'CPT_0': 0.5, 'GATE': 'OR', 'PROB': 0.7, 'PNODE_0': 'Cloudy'}, 
>  3: {'NODE': 'WETGRASS', 'CPT_3': 0.01, 'CPT_2': 0.1, 'CPT_1': 0.1, 'CPT_0': 1.0, 'GATE': 'OR', 'PROB': 0.5, 'PNODE_0': 'Rain', 'PNODE_1': 'Sprinkler'}
> }

I want to see the final result of:

print string
> ['CLOUDY', 'RAIN', 'SPRINKLER', 'WETGRASS']

So far, I have written this to get an output exactly as the one above.

string = "["
for value in xrange (0, len(node)):
    string += "'" + node[value].get("NODE") + "', "
string = string[:-2]
string += "]"
print string

I was wrapping my head around extracting all values from a dictionary within a dictionary that has a key of "NODE". Is that possible?

Padraic Cunningham

Just use the values from the dict:

print([d["NODE"] for d in node.values()])
['CLOUDY', 'RAIN', 'SPRINKLER', 'WETGRASS']

If the key might not be in all dicts just use in to check first:

print([d["NODE"] for d in node.values() if "NODE" in d])

node.values() is going to give you all the dicts:

[{'NODE': 'CLOUDY', 'GATE': 'EQUAL', 'PROB': 0.5}, {'NODE': 'RAIN', 'CPT_1': 0.2, 'CPT_0': 0.8, 'GATE': 'OR', 'PROB': 0.5, 'PNODE_0': 'Cloudy'}, {'NODE': 'SPRINKLER', 'CPT_1': 0.9, 'CPT_0': 0.5, 'GATE': 'OR', 'PROB': 0.7, 'PNODE_0': 'Cloudy'}, {'NODE': 'WETGRASS', 'CPT_3': 0.01, 'CPT_1': 0.1, 'CPT_0': 1.0, 'CPT_2': 0.1, 'GATE': 'OR', 'PROB': 0.5, 'PNODE_0': 'Rain', 'PNODE_1': 'Sprinkler'}]

So you just extract from each.

If you actually want to form a string that looks like a list:

 s = "[{}]".format(",".join([d["NODE"] for d in node.values() if "NODE" in d]))

Or simple:

print(str([d["NODE"] for d in node.values() if "NODE" in d]))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Accessing dictionary key values within the same dictionary

From Dev

Accessing dictionary key values within the same dictionary

From Dev

Grab 2 keys with highest values for nested dictionary

From Dev

Grab 2 keys with highest values for nested dictionary

From Dev

Comparing values of a list within a dictionary

From Dev

Accessing dictionary within dictionary

From Dev

Python: Dictionary within dictionary?

From Dev

Parsing Dictionary within a Dictionary

From Dev

Dictionary calls within dictionary

From Dev

Accessing dictionary within dictionary

From Dev

Handling tuples as values within a dictionary (in list comprehensions)

From Dev

Pythonic way to add values to a set within a dictionary

From Dev

How to get values nested within dictionary in NSDictionary?

From Dev

Search within a dictionary with a lot of values in python

From Dev

Dictionary within Dictionary in C#

From Dev

Reach dictionary data within dictionary

From Dev

Getting the a dictionary item within a dictionary

From Dev

Getting a list of values from dictionary when values are tuples within lists?

From Java

Ansible - Getting List Values from within a Dictionary (Regsiter Variable)

From Dev

Python adding dictionary values with same key within a list

From Dev

Get counts of specific values within a nested Python dictionary

From Dev

Python: Best way to keep track of particular values within dictionary

From Dev

Get counts of specific values within a nested Python dictionary

From Dev

foreach Dictionary<>.Values or foreach Dictionary<>

From Dev

foreach Dictionary<>.Values or foreach Dictionary<>

From Dev

Reference a dictionary within itself

From Dev

Sorting decimals within a dictionary

From Dev

Updating a list within a dictionary

From Dev

Append to a list within a dictionary

Related Related

  1. 1

    Accessing dictionary key values within the same dictionary

  2. 2

    Accessing dictionary key values within the same dictionary

  3. 3

    Grab 2 keys with highest values for nested dictionary

  4. 4

    Grab 2 keys with highest values for nested dictionary

  5. 5

    Comparing values of a list within a dictionary

  6. 6

    Accessing dictionary within dictionary

  7. 7

    Python: Dictionary within dictionary?

  8. 8

    Parsing Dictionary within a Dictionary

  9. 9

    Dictionary calls within dictionary

  10. 10

    Accessing dictionary within dictionary

  11. 11

    Handling tuples as values within a dictionary (in list comprehensions)

  12. 12

    Pythonic way to add values to a set within a dictionary

  13. 13

    How to get values nested within dictionary in NSDictionary?

  14. 14

    Search within a dictionary with a lot of values in python

  15. 15

    Dictionary within Dictionary in C#

  16. 16

    Reach dictionary data within dictionary

  17. 17

    Getting the a dictionary item within a dictionary

  18. 18

    Getting a list of values from dictionary when values are tuples within lists?

  19. 19

    Ansible - Getting List Values from within a Dictionary (Regsiter Variable)

  20. 20

    Python adding dictionary values with same key within a list

  21. 21

    Get counts of specific values within a nested Python dictionary

  22. 22

    Python: Best way to keep track of particular values within dictionary

  23. 23

    Get counts of specific values within a nested Python dictionary

  24. 24

    foreach Dictionary<>.Values or foreach Dictionary<>

  25. 25

    foreach Dictionary<>.Values or foreach Dictionary<>

  26. 26

    Reference a dictionary within itself

  27. 27

    Sorting decimals within a dictionary

  28. 28

    Updating a list within a dictionary

  29. 29

    Append to a list within a dictionary

HotTag

Archive