获取字典的值:
for value in DictName.values():
# value的名字可以自行另取
# DictName是要遍历的字典的名称
# .values():是固定的用法
获取键值:
for k,v in DictName.items():
#遍历字典的键值对,k对应键,v对应值
#k,v 的名字可以自己取,DictName是字典名
Python获取字典键对应的值有两种方法
当知道字典的键时:
unit_rooms={ 3:{301:[1,80],302:[1,80],303:[2,90],304:[2,90]},
4:{401:[1,80],402:[1,80],403:[2,90],404:[2,90]},
5:{501:[1,80],502:[1,80],503:[2,90],504:[2,90]}
}
for i in range(3,6):
rooms=unit_rooms[i]
print(rooms)
运行结果:
当不知道字典的键时:
unit_rooms = {3:{301:[1,80],302:[1,80],303:[2,90],304:[2,90]},
4:{401:[1,80],402:[1,80],403:[2,90],404:[2,90]},
5:{501:[1,80],502:[1,80],503:[2,90],504:[2,90]}
}
for rooms in unit_rooms.values():
print(rooms)
运行结果: