python中字典的操作

时间:2022-12-18 12:14:30

字典

# key-value
# 没有序号
info = {
    'stu1101':'Zhangsan',
    'stu1102':'Lisi',
    'stu1103':'Wangwu',
}
print(info)
print(info['stu1101'])       # 查
info['stu1101'] = '武藤兰'   # 修改
info['stu1104'] = '小苍'     # 没有就是直接添加了
print(info['stu1101'])
del info['stu1101']          # 删除
print(info)

info_1 = {
    'stu1101':'Zhangsan',
   'xuesheng':{ 'stu1102':'Lisi',
    'stu1103':'Wangwu'},
}
print (info_1['xuesheng']['stu1102'])   # 两层字典的用法

c = dict.fromkeys([1,2,3],"yanfeixu")   # 初始化一个新的字典
print(c)

for i in info:
    print(i,info[i])     # 最简便的循环方式

python中字典的操作