我有一个从JSON文档创建的字典:
{
"response": {
"version":"0.1",
"termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
"features": {
"hourly": 1
}
}
,
"hourly_forecast": [
{
"FCTTIME": {
"hour": "22","hour_padded": "22","min": "00","min_unpadded": "0","sec": "0","year": "2017","mon": "3","mon_padded": "03","mon_abbrev": "Mar","mday": "8","mday_padded": "08","yday": "66","isdst": "0","epoch": "1489028400","pretty": "10:00 PM EST on March 08, 2017","civil": "10:00 PM","month_name": "March","month_name_abbrev": "Mar","weekday_name": "Wednesday","weekday_name_night": "Wednesday Night","weekday_name_abbrev": "Wed","weekday_name_unlang": "Wednesday","weekday_name_night_unlang": "Wednesday Night","ampm": "PM","tz": "","age": "","UTCDATE": ""
},
"temp": {"english": "50", "metric": "10"},
"dewpoint": {"english": "20", "metric": "-7"},
"condition": "Partly Cloudy",
"icon": "partlycloudy",
"icon_url":"http://icons.wxug.com/i/c/k/nt_partlycloudy.gif",
"fctcode": "2",
"sky": "6",
"wspd": {"english": "29", "metric": "47"},
"wdir": {"dir": "W", "degrees": "262"},
"wx": "Clear/Wind",
"uvi": "0",
"humidity": "30",
"windchill": {"english": "-9999", "metric": "-9999"},
"heatindex": {"english": "-9999", "metric": "-9999"},
"feelslike": {"english": "50", "metric": "10"},
"qpf": {"english": "0.0", "metric": "0"},
"snow": {"english": "0.0", "metric": "0"},
"pop": "0",
"mslp": {"english": "30.01", "metric": "1016"}
}
,
{
"FCTTIME": {
"hour": "23","hour_padded": "23","min": "00","min_unpadded": "0","sec": "0","year": "2017","mon": "3","mon_padded": "03","mon_abbrev": "Mar","mday": "8","mday_padded": "08","yday": "66","isdst": "0","epoch": "1489032000","pretty": "11:00 PM EST on March 08, 2017","civil": "11:00 PM","month_name": "March","month_name_abbrev": "Mar","weekday_name": "Wednesday","weekday_name_night": "Wednesday Night","weekday_name_abbrev": "Wed","weekday_name_unlang": "Wednesday","weekday_name_night_unlang": "Wednesday Night","ampm": "PM","tz": "","age": "","UTCDATE": ""
},
"temp": {"english": "49", "metric": "9"},
"dewpoint": {"english": "20", "metric": "-7"},
"condition": "Partly Cloudy",
"icon": "partlycloudy",
"icon_url":"http://icons.wxug.com/i/c/k/nt_partlycloudy.gif",
"fctcode": "2",
"sky": "5",
"wspd": {"english": "24", "metric": "39"},
"wdir": {"dir": "W", "degrees": "263"},
"wx": "Clear/Wind",
"uvi": "0",
"humidity": "32",
"windchill": {"english": "-9999", "metric": "-9999"},
"heatindex": {"english": "-9999", "metric": "-9999"},
"feelslike": {"english": "49", "metric": "9"},
"qpf": {"english": "0.0", "metric": "0"},
"snow": {"english": "0.0", "metric": "0"},
"pop": "0",
"mslp": {"english": "30.02", "metric": "1017"}
}
有了这些数据,我想从'hourly_forecast'列表中的第一个字典中获取'temp'值,其中'hour'为'22'。
我知道我可以使用下面的代码来获取第一个元素:
j_hour['hourly_forecast'][0]['temp']['english']
...但是这些数据总是根据我检索它的时间而变化,所以如果它是下午1点,它将首先"hour": "13"
。
基本上我需要帮助,根据'小时'值来提取'temp'数据,并且列表总是在变化,所以我不能只根据元素序列来拉它。
总结一下,我从“我想要的'小时'开始打印”临时“键。
答案 0 :(得分:2)
解决此问题的一个好方法是逐步缩小搜索范围,直到获得您正在寻找的信息为止。
首先,使用list comprehension获取所有英国温度,无论小时数:
>>> [d['temp']['english'] for d in j_hour['hourly_forecast']]
['50', '49', ...]
然后将其限制为小时为'22'的情况:
>>> [d['temp']['english'] for d in j_hour['hourly_forecast']
... if d['FCTTIME']['hour'] == '22']
['50']
现在你已经得到了你想要的温度,包裹在列表中。如果您使用括号[]
替换括号()
将列表推导转换为generator expression,则可以在其上调用next()
以获取第一个(仅限于此案例):
>>> next(d['temp']['english'] for d in j_hour['hourly_forecast']
... if d['FCTTIME']['hour'] == '22')
'50'
...etvoilà!