首页 > 解决方案 > 如何使用 Python 3 从列表中选择一个或多个项目

问题描述

我正在学习 Python 3 并试图回答以下问题,该问题要求在最后填写一行代码。我用粗体表示,但需要帮助来理解我做错了什么。我阅读了 StackOverflow 上发布的类似问题,但似乎都比我想要做的更高级。谢谢你。

请填写空白,以便以下代码成功遍历存储在“makeup_products”中的数据,并提取仅用于“Lips”的项目的阴影总数,并将总数存储在名为total_count_lip_shades.

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

标签: pythonlist

解决方案


您只需要一个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

推荐阅读