赞
踩
本文介绍的是字典与列表相互嵌套时,该怎么遍历
# 生成一个列表
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)
输出结果:
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)
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}")
输出结果:
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
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}")
输出结果:
学生:xiaotian,学号:12,得分:88
学生:xiaoming,学号:13,得分:99
学生:xiaohong,学号:14,得分:77
sports = {
"xiaotian": ["篮球","足球"],
"xiaoming": ["羽毛","足球","篮球"],
"xiaohong": ["篮球","乒乓球"],
}
for student,sport in sports.items():
print(f"学生:{student},喜欢的运动有:{sport}")
输出结果:
学生:xiaotian,喜欢的运动有:[‘篮球’, ‘足球’]
学生:xiaoming,喜欢的运动有:[‘羽毛’, ‘足球’, ‘篮球’]
学生:xiaohong,喜欢的运动有:[‘篮球’, ‘乒乓球’]
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}")
输出结果:
学生:xiaotian,学号为:12,得分:88
学生:xiaoming,学号为:13,得分:99
学生:xiaohong,学号为:14,得分:77
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': '猪猪侠的手表'}
混合嵌套的遍历:根据切片器,先定位到目标所在的
for name in zidian["MinOrderType"]["keyposswr"][0]["onedisty"]:
leg, hand = name["leg"], name["hand"]
print(leg, hand)
# 输出结果:
# 猪猪侠的腿 猪猪侠的手
# 煎饼侠的腿 煎饼侠的手
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。