所以它是這樣的,當我輸入一個查詢時,它被解析和兩個值并拉出這是日期和指示器,并最初獲得該日期的適當響應response = {'l_result': [{'deaths': 5, 'tested': 5,}]}if value('DATE'): print("===================") print("this is the response ") print(response) print("===================")if value('indicator'):mapped_indicator = values('indicator')[-1] if mapped_indicator == 'Tested': tested_list = [ {'total_tested': response.get('tested'), 'new_tests': response.get('new_tests')}] print(tested_list)我的問題是所有代碼都運行。我希望,如果僅找到日期,則將顯示響應中的所有信息,如果找到日期和指示器,則僅顯示該指示器的特定信息。我的輸出是===================this is the response{'l_result': [{'deaths': 5, 'tested': 5,}]}===================tested: 5正如你所看到的,整個代碼的結果就在那里,我想要的只是tested: 5你能告訴我該怎么做嗎
2 回答

慕斯709654
TA貢獻1840條經驗 獲得超5個贊
只是給了你一個簡單的例子,希望這對你的情況有幫助,這不是字典結構
date = '10/19/2020'
indicator = 'Y'
if indicator =='Y' and date == '10/19/2020':
print('Indicator is ',indicator)
elif date == '10/19/2020':
print('Only date has a value')
print('2nd case when indicator is empty')
date = '10/19/2020'
indicator = None
if indicator =='Y' and date == '10/19/2020':
print('Indicator is ',indicator)
elif date == '10/19/2020':
print('Only date has a value')

慕慕森
TA貢獻1856條經驗 獲得超17個贊
從您提供的信息來看,您似乎必須使用邏輯運算“and”,然后有一個單獨的 for
if value('DATE'): 和 value('indicator')
埃利夫……
添加回答
舉報
0/150
提交
取消