我想將我的字典中的數字更改爲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
我希望能夠以數字的字典轉換爲整數繼續使用。任何幫助非常感謝。
你最後一個問題完全重複!你需要開始upvoting和接受!如果你真的試圖做一些學習,你從一個問題獲得代碼並粘貼到下一個..可能會有所幫助。 –