当前位置:   article > 正文

python基础——字典与列表相互嵌套的羁绊,怎么遍历呢?_python列表嵌套字典遍历取值

python列表嵌套字典遍历取值

本文介绍的是字典与列表相互嵌套时,该怎么遍历

1、遍历单个列表

# 生成一个列表
numbers = list(range(11))
# 遍历列表
for i in numbers:
   print("序号:", i)
   
# 另一种写法
li = [i for i in range(10)]
# enumerate(),将一个遍历的对象组合成一个可索引的序列,同时列出数据和下标
for i in enumerate(li):
    print("序号:",i)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

输出结果:
N1序号: 0
N1序号: 1
N1序号: 2
N1序号: 3
N1序号: 4
N1序号: 5
N1序号: 6
N1序号: 7
N1序号: 8
N1序号: 9
N1序号: 10

N2序号: (0, 0)
N2序号: (1, 1)
N2序号: (2, 2)
N2序号: (3, 3)
N2序号: (4, 4)
N2序号: (5, 5)
N2序号: (6, 6)
N2序号: (7, 7)
N2序号: (8, 8)
N2序号: (9, 9)
N2序号: (10, 10)

2、遍历字典键值对(items)、key值(keys)、values值(values)

dict_data = {
    "xiaotian": 101,
    "xiaoming": 102,
    "xiaohong": 103,
    "xiaolizi": 104
}
for key, values in dict_data.items():
    print(f"name: {key} , id: {values}")
for key in dict_data.keys():
    print(f"key:{key}")
for values in dict_data.values():
    print(f"value:{values}")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

输出结果:
name: xiaotian , id: 101
name: xiaoming , id: 102
name: xiaohong , id: 103
name: xiaolizi , id: 104

key:xiaotian
key:xiaoming
key:xiaohong
key:xiaolizi

value:101
value:102
value:103
value:104

3、列表里嵌套字典

students = [
    {"id": 12, "name": "xiaotian", "grades": 88},
    {"id": 13, "name": "xiaoming", "grades": 99},
    {"id": 14, "name": "xiaohong", "grades": 77},
]
for student in students:
    id, name, grades = student["id"] , student["name"] , student["grades"]
    print(f"学生:{name},学号:{id},得分:{grades}")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

输出结果:
学生:xiaotian,学号:12,得分:88
学生:xiaoming,学号:13,得分:99
学生:xiaohong,学号:14,得分:77

4、字典里嵌套列表

sports = {
    "xiaotian": ["篮球","足球"],
    "xiaoming": ["羽毛","足球","篮球"],
    "xiaohong": ["篮球","乒乓球"],
}
for student,sport in sports.items():
    print(f"学生:{student},喜欢的运动有:{sport}")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

输出结果:
学生:xiaotian,喜欢的运动有:[‘篮球’, ‘足球’]
学生:xiaoming,喜欢的运动有:[‘羽毛’, ‘足球’, ‘篮球’]
学生:xiaohong,喜欢的运动有:[‘篮球’, ‘乒乓球’]

4、字典里嵌套字典

informations = {
    "xiaotian": {"id": 12, "grades": 88},
    "xiaoming": {"id": 13, "grades": 99},
    "xiaohong": {"id": 14, "grades": 77},
}
for name,info in informations.items():
    id , grades = info["id"] , info["grades"]
    print(f"学生:{name},学号为:{id},得分:{grades}")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

输出结果:
学生:xiaotian,学号为:12,得分:88
学生:xiaoming,学号为:13,得分:99
学生:xiaohong,学号为:14,得分:77

5、混合嵌套

zidian = {
        "name": "护脸霜",
        "innerCode": "dsjj1154",
        "clusterSn": {"dataSourceType": "regular" , "bizSystem": "RDRSEW"},
        "description": "<p>1234567</p>",
        "skuOptions": [{"id": 1, "name": "颜色", "naed": "尺码", "values": ["黑色", "白色"]}],
        "categoryId": "343b5dbff0654d779fd81f4d8259ce60",
        "MinOrderType":{
            "keywords": "difhehdio",
            "keyposswr": [
                {"userName": "18110522", "onedisty":
                    [
                        {"leg": "猪猪侠的腿", "hand": "猪猪侠的手"},
                        {"leg": "煎饼侠的腿", "hand": "煎饼侠的手"}
                    ]
                    , "pwegdt": "猪猪侠的手表"
                 }],

            "gbCode": "GBM852741",
            "tableName": "test_table01",
        }
    }


print(type(zidian))
# 输出结果:<class 'dict'>
print(zidian["categoryId"])
# 输出结果:343b5dbff0654d779fd81f4d8259ce60
print(zidian["name"])
# 输出结果:护脸霜
print(zidian["clusterSn"]["bizSystem"])
# 输出结果:RDRSEW
print(zidian["skuOptions"][0]["values"])
# 输出结果:['黑色', '白色']
print(zidian["MinOrderType"]["keyposswr"][0])
# 输出结果:{'userName': '18110522', 'onedisty': [{'zhu_leg': '猪猪侠的腿', 'zhu_hand': '猪猪侠的手'}], 'pwegdt': '猪猪侠的手表'}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

混合嵌套的遍历:根据切片器,先定位到目标所在的

for name in zidian["MinOrderType"]["keyposswr"][0]["onedisty"]:
    leg, hand = name["leg"], name["hand"]
    print(leg, hand)
# 输出结果:
# 猪猪侠的腿 猪猪侠的手
# 煎饼侠的腿 煎饼侠的手
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/201832
推荐阅读
相关标签
  

闽ICP备14008679号