#!/usr/bin/env python#-*-coding utf8-*-#Author:caojininfo = { ‘stu1001‘: ‘caojin‘, ‘stu1002‘: ‘zhaoming‘, ‘stu1003‘: ‘zhansan‘, ‘stu1004‘: ‘lisi‘,}print(info)print(info[‘stu1002‘])info[‘stu1003‘] = "张山" #修改print(info)info[‘st112‘] = "孙悟空" #如果没有则是添加print(info)del info[‘stu1004‘] #删除print(info)info.pop(‘stu1003‘) #删除print(info)info.popitem() #随机删除print(info)print(info.get(‘stu00010‘)) #找元素,即便没有也没有错,出现noneprint(‘stu1001‘ in info) #判断元素是否在print(‘stu1005‘ in info) info.setdefault(‘stu1002‘,"贝吉塔") #先去找1002这个元素,如果有,直接返回值;如果没有,则增加新的值print(info) b = { ‘stu1001‘:‘lengmengyun‘, 1:3, 3:5}print(b)info.update(b) #把列表b的元素放进info里面,如果有同样的序列,则元素被替换。交叉的覆盖了print(info)print(info.items()) #把字典转成了列表c = info.fromkeys([6,7,8])print(c)e = dict.fromkeys([6,7,8],"100") #创建相同元素print(e)f = dict.fromkeys([6,7,8],[1,{"name":"caojin"},444])print(f)f[8][1][‘name‘] = "zhaoming" #注意用fromkeys创建字典的守护,改一个则都改了print(f)print("-----",info,"------")for i in info: #两个for效果一样,但是上面个高效,如果数据量上百万,下面个for循环可能会崩溃 print(i,info[i])for k,v in f.items(): print(k,v)
时间: 2024-10-27 07:00:26