python3中字典与列表相互嵌套取值

如何从字典中取值?

如何从字典中的列表中取值?

如何从字典的列表的字典中取值?

实例代码:

dian = {
        "name":"护脸霜",
        "innerCode":"",
        "clusterSn":{
            "dataSourceType": "regular",
            "bizSystem": "RDRSEW",
                },
        "description":"<p>1234567</p>",
        "skuOptions":[
                {
                "id":1,
                "name":"颜色",
                "naed":"尺码",
                "values":[
                    "黑色"
                    ]
            }
        ],
        "categoryId":"343b5dbff0654d779fd81f4d8259ce60",
        "MinOrderType":{
            "keywords": "",
            "keyposswr":[
                {
                "userName": "18110522",
                "onedisty": [
                    {
                    "zhu_leg": "猪猪侠的腿",
                    "zhu_hand": "猪猪侠的手",
                }
            ],
                "pwegdt": "猪猪侠的手表"
                }
            ],
            "gbCode": "GBM852741",
            "tableName": "test_table01",
        }
    }

上边是一个字典 ,字典中包含列表,包含字典,还有列表中包含字典。对于该如何取值:

 1 print(type(dian))
 2 print(dian["categoryId"])
 3 print(type(dian["categoryId"]))         # 取出的是字符串
 4 print(dian["clusterSn"]["bizSystem"])
 5 print(dian["skuOptions"])               # 取出的是list
 6 print(type(dian["skuOptions"]))
 7 print(dian["skuOptions"][0])            # 取出的是dict
 8 print(type(dian["skuOptions"][0]))
 9 print(dian["skuOptions"][0]["name"])    # 取dict中list中包含的dict指定的值
10 print(dian["MinOrderType"]["keyposswr"][0]["onedisty"][0]["zhu_leg"])

执行结果如下:

 

 

 

 

 

posted @ 2019-11-17 14:33  漂泊的小虎  阅读(6167)  评论(0编辑  收藏  举报