我想将字典中的数字更改为 int 值,以便稍后在我的程序中使用。到目前为止我有
import time
import math
x = 400
y = 300
def read_next_object(file):
obj = {}
for line in file:
if not line.strip(): continue
line = line.strip()
key, val = line.split(": ")
if key in obj and key == "Object":
yield obj
obj = {}
obj[key] = val
yield obj
planets = {}
with open( "smallsolar.txt", 'r') as f:
for obj in read_next_object(f):
planets[obj["Object"]] = obj
print(planets)
scale=250/int(max([planets[x]["Orbital Radius"] for x in planets if "Orbital Radius" in planets[x]]))
print(scale)
输出是
{'Sun': {'Object': 'Sun', 'Satellites': 'Mercury,Venus,Earth,Mars,Jupiter,Saturn,Uranus,Neptune,Ceres,Pluto,Haumea,Makemake,Eris', 'Orbital Radius': '0', 'RootObject': 'Sun', 'Radius': '20890260'}, 'Moon': {'Object': 'Moon', 'Orbital Radius': '18128500', 'Period': '27.321582', 'Radius': '1737000.10'}, 'Earth': {'Object': 'Earth', 'Satellites': 'Moon', 'Orbital Radius': '77098290', 'Period': '365.256363004', 'Radius': '6371000.0'}}
3.2426140709476178e-06
我希望能够将 dict 中的数字转换为 ints 以供进一步使用。非常感谢任何帮助。