Python 访问变量';s信息
我正在创建一个工具来查找新墨西哥州两个地区的温度。在数据集中,字典(Python 访问变量';s信息,python,dictionary,for-loop,Python,Dictionary,For Loop,我正在创建一个工具来查找新墨西哥州两个地区的温度。在数据集中,字典(main)中有一个温度变量,我似乎无法获取程序的内容。输出应该是288.142,是的,我知道这是用开尔文表示的 NMNorthzip=[87401,87301] for x in NMNorthzip: r = requests.get("http://api.openweathermap.org/data/2.5/forecast?zip=" +str(x) + ",us&a
main
)中有一个温度变量,我似乎无法获取程序的内容。输出应该是288.142
,是的,我知道这是用开尔文表示的
NMNorthzip=[87401,87301]
for x in NMNorthzip:
r = requests.get("http://api.openweathermap.org/data/2.5/forecast?zip="
+str(x) + ",us&appid=id")
data = r.json()
forecast = data['list']
for n in forecast:
main = n['main']
for z in main.items():
print(z)
输出:
('temp_max',288.142)
(‘海平面’,1028.46)
('grnd_level',797.18)
('temp_min',288.142)
('temp',288.142)
(‘湿度’,26)
('temp_kf',0)
(“压力”,797.18)
您正在将字典转换为列表,并打印由.items()
函数生成的(键,对)元组。因此,请删除以下内容:
for z in main.items():
print(z)
而是访问字典项:
temperature = main['temp']
你是说
打印(主['temp'])
?是的!非常感谢。德鲁:我想我该收工了。请读一读。