如何使用Python 3从列表中选择一个或多个项目

2024-04-29 10:45:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习Python3,并试图回答下面的问题,这个问题要求在最后填写一行代码。我用粗体写了一行,但需要帮助才能理解我做错了什么。我在StackOverflow上看到了类似的问题,但这些问题似乎都比我想做的要高级。多谢各位

请填入空白,以便下面的代码成功地迭代存储在“MaMuppi产品”中的数据,并提取仅用于“嘴唇”的项目的阴影总数,将总数存储在一个名为^ {CD1>}的变量中。

makeup_products = {"Products": [
                    {"Primer": {
                               "Shades": 15, 
                               "Styles": 5,
                               "Location": "Face"
                            }
                    },
                    {"Lipstick": {
                               "Shades": 48, 
                               "Styles": 3,
                                "Location": "Lips"
                              }
                    },
                    {"Lip liner": {
                               "Shades": 32, 
                               "Styles": 4,
                                "Location": "Lips"
                               }
                    },
                    {"Blush": {
                            "Shades": 13, 
                            "Styles": 2,
                            "Location": "Face"
                          }
                    },
                    {"Eye Liner": {
                               "Shades": 14, 
                               "Styles": 7,
                               "Location": "Eye"
                               }
                    },
                    {"Travel Makeup Kit": {
                               "Shades": "N/A",
                               "Styles": 3,
                               "Location": "Face, Lips, Eye"
                               }
                    }
                 ] }


total_count_lip_shades = 0
for item in makeup_products["Products"]:
    for product in item:
       **IF ITEM[PRODUCT]["LIP STICK", "LIP LINER"]**
            shade_count = item[product]["Shades"]
            total_count_lip_shades += shade_count

Tags: 代码countlocationitemproductsfacetotaleye
1条回答
网友
1楼 · 发布于 2024-04-29 10:45:16

您只需要一个if语句来检查Location值是否等于Lips

makeup_products = {"Products": [
                    {"Primer": {
                               "Shades": 15, 
                               "Styles": 5,
                               "Location": "Face"
                            }
                    },
                    {"Lipstick": {
                               "Shades": 48, 
                               "Styles": 3,
                                "Location": "Lips"
                              }
                    },
                    {"Lip liner": {
                               "Shades": 32, 
                               "Styles": 4,
                                "Location": "Lips"
                               }
                    },
                    {"Blush": {
                            "Shades": 13, 
                            "Styles": 2,
                            "Location": "Face"
                          }
                    },
                    {"Eye Liner": {
                               "Shades": 14, 
                               "Styles": 7,
                               "Location": "Eye"
                               }
                    },
                    {"Travel Makeup Kit": {
                               "Shades": "N/A",
                               "Styles": 3,
                               "Location": "Face, Lips, Eye"
                               }
                    }
                 ] }


total_count_lip_shades = 0
for item in makeup_products["Products"]:
    for product in item:
       if item[product]["Location"] == "Lips":
            shade_count = item[product]["Shades"]
            total_count_lip_shades += shade_count

相关问题 更多 >