我正在寫if語句來確定我的字典是否有多個鍵。如果不是,我只是想通過。我想如果下面的代碼沒有找到「紅移」作爲它會通過的關鍵。但是,當我運行它紅移不是一個關鍵,它不通過。我收到一個重要錯誤。問題是什麼。只要它發現「紅移」或「主人」,它就不會通過。檢查字典中的多個鍵不起作用。 python
print config[name].keys()
if ("redshift" and "host") in config[name]:
print config[name].keys()
name.append(config[name]["name"])
redshift.append(config[name]["redshift"][0]['value'])
type.append(config[name]["type"][0]["value"])
else:
pass
什麼的'值這裏config'?請舉一個例子。 – AKS
你正在循環使用'在配置[名稱]爲la:'但從未在循環內使用'la'一次? – AKS
config以表單的.json文件形式調用。一個例子是,config [SNname] .keys = [u'discoverer',u'redshift',u'claimedtype',u'sources',u'maxabsmag',u'aliases',u'hostdec',u'comovingdist ',u'maxappmag',u'maxdate',u'ra',u'hostra',u'lumdist',u'maxband',u'host',u'discoverdate',u'dec',u'name ',u'spectra',u'ebv',u'velocity',u'hothotometry'] – astrochris