3 Replies - 1359 Views - Last Post: 23 November 2012 - 02:45 AM Rate Topic: -----

#1 medaa  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 24-October 12

Iterating thru nested dictionaries question.

Posted 22 November 2012 - 06:00 PM

Can someone please show me how to iterate over a nested dictionary, I have many questions to answer, but I just dont get how to do this?

Planets={
    'Mercury':{
               'Radius':2439.7,
               'Distance from the sun':58,
               'Moons':[],
               'Atmosphere?':'True',
               'Gas planet?':'False',
    },
    'Venus':{
        'Radius':6051.8,
        'Distance from the sun':108,
        'Moons':[],
        'Atmosphere?':'True',
        'Gas planet?':'False'
    },
    'Earth':{
        'Radius':'6371.0',
        'Distance from the sun':'150',
        'Moons':['Moon'],
        'Atmosphere?':'True',
        'Gas planet?':'False'
    },
    'Mars':{
        'Radius':3396.2,
        'Distance from the sun':207,
        'Moons':['Phobos','Deimos'],
        'Atmosphere?':'True',
        'Gas planet?':'False'
    },
    'Jupiter':{
        'Radius':69911,
        'Distance from the sun':483,
        'Moons':['Io', 'Ganymede', 'Callisto', 'Europa', 'Adrastea'],
        'Atmosphere?':'True',
        'Gas planet?':'True'
    },
    'Saturn':{
        'Radius':60268,
        'Distance from the sun':1400,
        'Moons':['Pan', 'Prometheus', 'Titan', 'Phoebe', 'Rhea'],
        'Atmosphere?':'True',
        'Gas planet?':'True'
    },
    'Uranus':{
        'Radius':25559,
        'Distance from the sun':3000,
        'Moons':['Miranda', 'Ariel', 'Umbriel', 'Titania', 'Oberon'],
        'Atmosphere?':'True',
        'Gas planet?':'True' 
    },
    'Neptune':{
        'Radius':24764,
        'Distance from the sun':4500,
        'Moons':['Triton', 'Nereid', 'Proteus', 'Naiad', 'Thalassa'],
        'Atmosphere?':'True',
        'Gas planet?':'True'
    },
}

for key in Planets.keys():
    print(key)
    print("\n".join(["{0}:{1}".format(k,v) for k,v in Planets[key].items()]))


for example how would i find, What planet is closest to the sun with at least 2 moons?, can someone please show me an example it would be really helpful

Is This A Good Question/Topic? 0
  • +

Replies To: Iterating thru nested dictionaries question.

#2 woooee  Icon User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 94
  • Joined: 21-November 12

Re: Iterating thru nested dictionaries question.

Posted 22 November 2012 - 07:01 PM

First, your dictionary is unnecessarily complex. Since each key contains the same items you can use a list
planet_dict = {'Mercury':[2439.7, 58, [], 'True', 'False']}
etc., so 'Gas Planet' would be offset 4 (or -1) in the list. To use what you have
planets={
	    'Mercury':{
	               'Radius':2439.7,
	               'Distance from the sun':58,
	               'Moons':[],
	               'Atmosphere?':'True',
	               'Gas planet?':'False',
	    },
	    'Venus':{
	        'Radius':6051.8,
	        'Distance from the sun':108,
	        'Moons':[],
	        'Atmosphere?':'True',
	        'Gas planet?':'False'
	    },
	    'Earth':{
	        'Radius':'6371.0',
	        'Distance from the sun':'150',
	        'Moons':['Moon'],
	        'Atmosphere?':'True',
	        'Gas planet?':'False'
	    },
	    'Mars':{
	        'Radius':3396.2,
	        'Distance from the sun':207,
	        'Moons':['Phobos','Deimos'],
	        'Atmosphere?':'True',
	        'Gas planet?':'False'
	    },
	    'Jupiter':{
	        'Radius':69911,
	        'Distance from the sun':483,
	        'Moons':['Io', 'Ganymede', 'Callisto', 'Europa', 'Adrastea'],
	        'Atmosphere?':'True',
	        'Gas planet?':'True'
	    },
	    'Saturn':{
	        'Radius':60268,
	        'Distance from the sun':1400,
	        'Moons':['Pan', 'Prometheus', 'Titan', 'Phoebe', 'Rhea'],
	        'Atmosphere?':'True',
	        'Gas planet?':'True'
	    },
	    'Uranus':{
	        'Radius':25559,
	        'Distance from the sun':3000,
	        'Moons':['Miranda', 'Ariel', 'Umbriel', 'Titania', 'Oberon'],
	        'Atmosphere?':'True',
	        'Gas planet?':'True'
	    },
	    'Neptune':{
	        'Radius':24764,
	        'Distance from the sun':4500,
	        'Moons':['Triton', 'Nereid', 'Proteus', 'Naiad', 'Thalassa'],
	        'Atmosphere?':'True',
	        'Gas planet?':'True'
	    }
}

for key in planets:
    print key, planets[key]['Gas planet?'] 
Also, please try to conform to the Python Style Guide as it makes code easier for others to understand.
Was This Post Helpful? 0
  • +
  • -

#3 medaa  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 24-October 12

Re: Iterating thru nested dictionaries question.

Posted 22 November 2012 - 08:15 PM

I wanted to find what planet had the moon Europa, I tried something like
 for key in Planets:
    print(key,planets[key]['Moons'=='Europa']
but I got an error. Im having alot of trouble with this.
Was This Post Helpful? 0
  • +
  • -

#4 Tayacan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 275
  • Joined: 18-January 11

Re: Iterating thru nested dictionaries question.

Posted 23 November 2012 - 02:45 AM

View Postmedaa, on 23 November 2012 - 03:15 AM, said:

I wanted to find what planet had the moon Europa, I tried something like
 
for key in Planets:
    print(key,planets[key]['Moons'=='Europa']


but I got an error. Im having alot of trouble with this.


Right. So, think about how your dictionary is structured.
planets[key]


Is a value from the planets dictionary, which is in itself a dictionary. So
planets[key]['Gas planet?']


Is a lookup in that dictionary. So to find out if a planet has the moon Europa, you'd need to find the relevant entry in the dictionary, which is a list, and then figure out if 'Europa' is in that list.

If you have a list, you can see if an item is in it like this:
if 'Europa' in ['Io', 'Ganymede', 'Callisto', 'Europa', 'Adrastea']:
    print ("Found Europa!")


It's exactly the same when you get the list by a dictionary lookup:
for key in planets:
    if 'Europa' in planets[key]['Moons']:
        print(key,planets[key])


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1