简体   繁体   English

访问嵌套字典 3 级深度 Python

[英]Accessing Nested Dictionary 3 levels deep Python

After my for loop successfully returns the keys of my first set of nested dictionaries.在我的 for 循环成功返回我的第一组嵌套字典的键之后。 I am getting this error:我收到此错误:

for item in MENU[drink][ingredient_list]:
TypeError: 'float' object is not iterable

I need to get access to all of the key-value pairs so that I can perform operations on them, but this code gets stuck at 'espresso'.我需要访问所有键值对,以便对它们执行操作,但此代码卡在“espresso”中。

 #3 levels deep nested dictionary
MENU = {
    "espresso": {
        "ingredients": {
            "water": 50,
            "coffee": 18,
        },
        "cost": 1.5,
    },
    "latte": {
        "ingredients": {
            "water": 200,
            "milk": 150,
            "coffee": 24,
        },
        "cost": 2.5,
    },
    "cappuccino": {
        "ingredients": {
            "water": 250,
            "milk": 100,
            "coffee": 24,
        },
        "cost": 3.0,
    }
}

#level one of the dictionary
for drink in MENU:
    #this should print the drinks and cost
    print(drink)
    #this should print the ingredients   
    for ingredient_list in MENU[drink]:
        print(ingredient_list)
        for item in MENU[drink][ingredient_list]: 
            print(item)

You are iterating over cost as well.您也在迭代cost Try this:尝试这个:

#level one of the dictionary
for drink, data in MENU.items():
    #this should print the drinks and cost
    print(drink, data["cost"])
    #this should print the ingredients   
    for ingredient in data["ingredients"].items():
        print(ingredient)

As Bharel suggested, if you just iterate the dict MENU python will by default iterate only the keys - this is why the code works if you just run:正如 Bharel 建议的那样,如果你只是迭代 dict MENU python 默认情况下只会迭代键 - 这就是为什么如果你只运行代码就可以工作的原因:

for drink in MENU:
   #this should print the drinks and cost
   print(drink)

But it will raise an exception when you try the second for loop, because you are not iterating the values, to iterate both values and keys use MENU.items() as Bharel suggested.但是,当您尝试第二个 for 循环时,它会引发异常,因为您没有迭代值,要迭代值和键,请按照 Bharel 的建议使用 MENU.items() 。

for key, value in MENU.items():
   #this should print the drinks and cost
   print(key, value["cost"])
   #this should print the ingredients   
   for key_i, value_i in value["ingredients"].items():
       print(key_i, value_i)

Your dictionary is not a complete 3 level one.您的字典不是完整的 3 级。 It has just 2 complete levels.它只有 2 个完整的级别。 For example MENU['espresso']['cost'] is equal to 1.5, so you can not iterate on it.例如 MENU['espresso']['cost'] 等于 1.5,因此您不能对其进行迭代。

MENU = {
    "espresso": {
        "ingredients": {
            "water": 50,
            "coffee": 18,
        },
        "cost": {
            "normal": 1.5,
            "vip": 4.5,
        }
    },
    "latte": {
        "ingredients": {
            "water": 200,
            "milk": 150,
            "coffee": 24,
        },
        "cost": {
            "normal": 2.5,
            "vip": 6.5,
        }
    },
    "cappuccino": {
        "ingredients": {
            "water": 250,
            "milk": 100,
            "coffee": 24,
        },
        "cost": {
            "normal": 3,
            "vip": 4.1,
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM