Python:嵌套字典问题:尝试运行'if'命令来打印出字符串



我正试图为这个嵌套字典做以下两件事:

  1. 如果一只鸟有攻击性,请打印出一条字符串,建议我们使用本打印声明中的"动作"列表"遮盖头部"。

  2. 如果一只鸟濒临灭绝,请打印出一条字符串,建议我们"后退",同时使用本打印声明中的行动列表。

这是我到目前为止所拥有的。非常感谢您的帮助!:

rarebirds = {
'Gold-crested Toucan': {
'Height (m)': 1.1,
'Weight (kg)': 35,
'Color': 'Gold',
'Endangered': True,
'Aggressive': True},
'Pearlescent Kingfisher': {
'Height (m)': 0.25,
'Weight (kg)': 0.5,
'Color': 'White',
'Endangered': False,
'Aggressive': False},
'Four-metre Hummingbird': {
'Height (m)': 0.6,
'Weight (kg)': 0.5,
'Color': 'Blue',
'Endangered': True,
'Aggressive': False},
'Giant Eagle': {
'Height (m)': 1.5,
'Weight (kg)': 52,
'Color': 'Black and White',
'Endangered': True,
'Aggressive': True},
'Ancient Vulture': {
'Height (m)': 2.1,
'Weight (kg)': 70,
'Color': 'Brown',
'Endangered': False,
'Aggressive': False}
}
actions = ['Back Away', 'Cover our Heads', 'Take a Photograph']

for key, value in rarebirds.items():
for value in value:
if value == 'Aggressive' and True:
print(key, ":", actions[1])
return
for key, value in rarebirds.items():
for value in value:
if value == 'Endangered' and True:
print(key, ":", actions[0])
return

已识别问题:

  1. 循环第二次出现缩进问题
  2. 不需要return语句
  3. if value == 'Aggressive' and True:-对于所有将Aggressive作为其值之一的密钥,无论是True还是False,此条件都将为真

由于rarebirdsdict,所以我们不想遍历value。它可以简单地写为,

for key, value in rarebirds.items():
if value['Aggressive'] == True:
print(key + ":" + actions[1])
for key, value in rarebirds.items():
if value['Endangered'] == True:
print(key + ":" + actions[0])

对于输出,

Gold-crested Toucan:Cover our Heads
Giant Eagle:Cover our Heads
Gold-crested Toucan:Back Away
Four-metre Hummingbird:Back Away
Giant Eagle:Back Away

另一种方法是将名称和操作序列构建为元组。

这里的目的是展示生成器的使用,这有助于避免构建额外的数据结构(注意,根据生成器的使用情况,甚至每个元组都可以被生成器替换(。

例如,假设你只能拍摄一张中性物种的照片:

rarebirds = {
'Gold-crested Toucan': {
'Height (m)': 1.1,
'Weight (kg)': 35,
'Color': 'Gold',
'Endangered': True,
'Aggressive': True},
'Pearlescent Kingfisher': {
'Height (m)': 0.25,
'Weight (kg)': 0.5,
'Color': 'White',
'Endangered': False,
'Aggressive': False},
'Four-metre Hummingbird': {
'Height (m)': 0.6,
'Weight (kg)': 0.5,
'Color': 'Blue',
'Endangered': True,
'Aggressive': False},
'Giant Eagle': {
'Height (m)': 1.5,
'Weight (kg)': 52,
'Color': 'Black and White',
'Endangered': True,
'Aggressive': True},
'Ancient Vulture': {
'Height (m)': 2.1,
'Weight (kg)': 70,
'Color': 'Brown',
'Endangered': False,
'Aggressive': False}
}
actions = ['Back Away', 'Cover our Heads', 'Take a Photograph']
def show_actions(birds):
for name, attrs in birds.items():
acs = []
if attrs['Aggressive']:
acs.append(actions[1])
if attrs['Endangered']:
acs.append(actions[0])
if not acs:
acs.append(actions[2])
yield (name, *acs)
for bird, *acs in show_actions(rarebirds):
print(f"{bird}: {', '.join(acs)}")

产生

Gold-crested Toucan: Cover our Heads, Back Away
Pearlescent Kingfisher: Take a Photograph
Four-metre Hummingbird: Back Away
Giant Eagle: Cover our Heads, Back Away
Ancient Vulture: Take a Photograph
final_actions={}
for key, value in rarebirds.items():
if final_actions.get(key)==None:
final_actions[key]=[]
if 'Aggressive' in value and value.get("Aggressive")==True:
final_actions[key].append(actions[1])
if 'Endangered' in value and value.get("Endangered")==True:
final_actions[key].append(actions[0])
if ('Aggressive' in value and value.get("Aggressive")==False) and ('Endangered' in value and value.get("Endangered")==False):
final_actions[key].append(actions[2])
print(final_actions)

你会得到的答案是:

{
"Four-metre Hummingbird":[
"Back Away"
],
"Giant Eagle":[
"Cover our Heads",
"Back Away"
],
"Gold-crested Toucan":[
"Cover our Heads",
"Back Away"
],
"Pearlescent Kingfisher":[
"Take a Photograph"
],
"Ancient Vulture":[
"Take a Photograph"
]
}

此外,

for value in value:
if value == 'Aggressive' and True:

上面的代码在您的代码中是错误的。相反,你可以尝试

for nested_value in value:
if nested_value == 'Aggressive' and value.get(nested_value)==True:

最新更新