十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
res = {'reason': '查詢成功',
'result': [{'wid': '00', 'weather': '晴'},
{'wid': '01', 'weather': '多云'},
{'wid': '02', 'weather': '陰'},
{'wid': '03', 'weather': '陣雨'},
{'wid': '04', 'weather': '雷陣雨'},
{'wid': '05', 'weather': '雷陣雨伴有冰雹'}],
'error_code': 0}
1、獲取響應(yīng)結(jié)果的鍵和值
key=res.keys()
value=res.values()
結(jié)果:
dict_keys(['reason', 'result', 'error_code']) dict_values(['查詢成功', [{'wid': '00', 'weather': '晴'}, {'wid': '01', 'weather': '多云'}, {'wid': '02', 'weather': '陰'}, {'wid': '03', 'weather': '陣雨'}, {'wid': '04', 'weather': '雷陣雨'}, {'wid': '05', 'weather': '雷陣雨伴有冰雹'}], 0])
2、獲取result 中每個(gè)字典中的key和value值
list=res['result']
for i in list:
for key ,value in i.items():
print(key ,value)
結(jié)果:
wid 00
weather 晴
wid 01
weather 多云
wid 02
weather 陰
wid 03
weather 陣雨
wid 04
weather 雷陣雨
wid 05
weather 雷陣雨伴有冰雹
3、取出wid“03”的天氣
{'wid': '03', 'weather': '陣雨'}
list[3]['weather']
結(jié)果:
陣雨
4、輸出數(shù)組中指定的值:
res={'reason': '查詢成功',
'result': [{'wid': '00', 'weather': '晴'},
{'wid': '01', 'weather': '多云'},
{'wid': '02', 'weather': '陰'},
{'wid': '03', 'weather': '陣雨'},
{'wid': '04', 'weather': '雷陣雨'},
{'wid': '05', 'weather': '雷陣雨伴有冰雹'}],
'error_code': 0}
查詢編號(hào)01的天氣是什么?
list=res['result']
for i in list:
if ("01" in i.values()):
print(list[list.index(i)]['weather'])
結(jié)果:
多云
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧