我正在尝试使用for循环在嵌套字典中添加项目。该项目应该在内部字典中,因此它在项目'Aggressive': True
之后显示为项目,并且在每个内部字典(即'Gold-crested Toucan'
和'Pearlescent Kingfisher'
中)。>
rarebirds = {
'Gold-crested Toucan': {
'Height (m)': 1.1,
'Weight (kg)': 35,
'Aggressive': True},
'Pearlescent Kingfisher': {
'Height (m)': 0.25,
'Weight (kg)': 0.5,
'Aggressive': False},
}
#my most recent attempt below, although I've tried using .update as well as i and j dics within the loop
for key in rarebirds:
rarebirds[key]['Seen'] == False
输出为KeyError: 'Seen'
对于这个简单问题的建议将不胜感激。
答案 0 :(得分:0)
rarebirds = {
"Gold-crested Toucan": {
"Height (m)": 1.1,
"Weight (kg)": 35,
"Aggressive": True
},
"Pearlescent Kingfisher": {
"Height (m)": 0.25,
"Weight (kg)": 0.5,
"Aggressive": False,
},
}
for key in rarebirds.keys():
rarebirds[key]['Seen'] = False
rarebirds.keys()
遍历键。rarebirds[key]['Seen'] == False
。双重等于正在尝试执行相等检查,这会导致keyError
。