Search code examples
dictionarypython-3.xextractscale

python3 finding/extracting the largest number from a dictionary


I have a dictionary with solar system data (e.g. orbital radius of planets and moons) and I want to find the largest orbital radius within my dictionary. I am going to use it later as a scale for an animation I'm doing. But something is wrong with my code and I'm not sure what's wrong?

scale = 600/max([planets[key]['Orbital Radius'] for key in planets])

My animation window (in QuickDraw) is 600x600 so I need to scale my orbital radius values but the code above doesn't seem to work. planets is my dictionary. heres's my dictionary:

{'Mercury': {'Orbital Radius': '38001200', 'Radius': '243900.7',
'Period': '87.9691'}, 'Ariel': {'Orbital Radius': '8595000', 'Radius':
'60000', 'Period': '2.520379'}, 'Sun': {'Satellites':
'Mercury,Venus,Earth,Mars,Jupiter,Saturn,Uranus,Neptune,Ceres,Pluto,Haumea,Makemake,Eris',
'Orbital Radius': '0', 'Radius': '20890260'}, 'Phobos': {'Orbital
Radius': '3623500.6', 'Radius': '200000', 'Period': '0.31891023'},
'Deimos': {'Orbital Radius': '8346000', 'Radius': '200000.2',
'Period': '1.26244'}, 'Mars': {'Satellites': 'Phobos,Deimos', 'Orbital
Radius': '106669000', 'Radius': '339600.2', 'Period': '686.971'},
'Rhea': {'Orbital Radius': '24000000', 'Radius': '75000', 'Period':
'4.5'}, 'Oberon': {'Orbital Radius': '26235000', 'Radius': '75000',
'Period': '13.463'}, 'Europa': {'Orbital Radius': '36486200',
'Radius': '156000.8', 'Period': '3.551181'}, 'Tethys': {'Orbital
Radius': '13706000', 'Radius': '50000', 'Period': '1.9'}, 'Miranda':
{'Orbital Radius': '5822550', 'Radius': '23500', 'Period': '1.413'},
'Saturn': {'Satellites':
'Mimas,Enceladus,Tethys,Dione,Rhea,Titan,Iapetus', 'Orbital Radius':
'353572956', 'Radius': '6026800', 'Period': '10759.22'}, 'Uranus':
{'Satellites': 'Puck,Miranda,Ariel,Umbriel,Titania,Oberon', 'Orbital
Radius': '453572956', 'Radius': '2555900', 'Period': '30799'},
'Neptune': {'Satellites': 'Triton', 'Orbital Radius': '550000000',
'Radius': '2476400', 'Period': '60190'}, 'Titania': {'Orbital Radius':
'19575000', 'Radius': '75000', 'Period': '8.7058'}, 'Enceladus':
{'Orbital Radius': '10706000', 'Radius': '25000', 'Period': '1.4'},
'Venus': {'Orbital Radius': '57477000', 'Radius': '605100.8',
'Period': '224.698'}, 'Moon': {'Orbital Radius': '18128500', 'Radius':
'173700.10', 'Period': '27.321582'}, 'Triton': {'Orbital Radius':
'40000000', 'Radius': '135300', 'Period': '-5.8'}, 'Ceres': {'Orbital
Radius': '130995855', 'Radius': '48700', 'Period': '1679.67'},
'Mimas': {'Orbital Radius': '8433396', 'Radius': '20600', 'Period':
'0.9'}, 'Titan': {'Orbital Radius': '50706000', 'Radius': '257600',
'Period': '15.945'}, 'Ganymede': {'Orbital Radius': '47160000',
'Radius': '263400', 'Period': '7.15455296'}, 'Umbriel': {'Orbital
Radius': '11983500', 'Radius': '60000', 'Period': '4.144177'},
'Callisto': {'Orbital Radius': '69700000', 'Radius': '241000',
'Period': '16.6890184'}, 'Jupiter': {'Satellites':
'Io,Europa,Ganymede,Callisto', 'Orbital Radius': '210573600',
'Radius': '7149200', 'Period': '4332.59'}, 'Io': {'Orbital Radius':
'22000000', 'Radius': '182100.3', 'Period': '1.7691377186'}, 'Earth':
{'Satellites': 'Moon', 'Orbital Radius': '77098290', 'Radius':
'637100.0', 'Period': '365.256363004'}, 'Dione': {'Orbital Radius':
'17106000', 'Radius': '56000', 'Period': '2.7'}, 'Iapetus': {'Orbital
Radius': '72285891', 'Radius': '75000', 'Period': '79'}}

Solution

  • All the values in your dictionary are strings so you are seeing the error:

    TypeError: unsupported operand type(s) for /: 'int' and 'str'

    This is telling you that you are taking an int (the value 600) and trying to divide it / by a string which obviously doesn't make sense. You need to convert the string to a float first by using float():

    scale = 600/float(max([planets[key]['Orbital Radius'] for key in planets]))

    Its needs to be a float and not an int because you are trying to scale down, so the value of scale will be less than 1 for example if the largest value is 1200 and your grid size is 600 you scale will be 0.5 (a half).