- 特性
- key:value结构,字典中的每一个元素,都是键值对
- key必须可被hash,且必须为不可变数据类型,必须唯一
- 可存放任意多个值,可修改,可以不唯一
- 无序
- 查找速度快,因为hash可以把key变成数字,数字则可通过二分法快速查找,提高查找效率
- 基本语法
- clear:清空字典
-
Info = {'stu1':'LiLei','stu2':'HanMeiMei','stu3':'XiaoMing'} print(Info) # =>{'stu1': 'LiLei', 'stu2': 'HanMeiMei', 'stu3': 'XiaoMing'} Info.clear() print(Info) # =>{}
- copy:复制字典
-
Info = {'stu1':'LiLei','stu2':'HanMeiMei','stu3':'XiaoMing'} Info_new = Info.copy() print(Info_new) # =>{'stu1': 'LiLei', 'stu2': 'HanMeiMei', 'stu3': 'XiaoMing'}
-
- fromkeys:返回一个新的字典,key值可迭代,但所有的value都是相等的
-
Info = {} Info_new = Info.fromkeys([1,2,3],'Hi') print(Info_new) # =>{1: 'Hi', 2: 'Hi', 3: 'Hi'}
- get:通过key获取对应的value,如果字典中key存在,则返回对应的value,如果不存在,返回None
-
Info = {'stu1':'LiLei','stu2':'HanMeiMei','stu3':'XiaoMing'} print(Info.get('stu2'))# =>HanMeiMei print(Info.get('stu4'))# =>None
- items:获取所有的key-value,以列表方式呈现
-
Info = {'stu1':'LiLei','stu2':'HanMeiMei','stu3':'XiaoMing'} print(Info.items())# =>dict_items([('stu1', 'LiLei'), ('stu2', 'HanMeiMei'), ('stu3', 'XiaoMing')])
- keys:获取所有的key
-
Info = {'stu1':'LiLei','stu2':'HanMeiMei','stu3':'XiaoMing'} print(Info.keys()) # =>dict_keys(['stu1', 'stu2', 'stu3'])
- values:获取所有的values
-
Info = {'stu1':'LiLei','stu2':'HanMeiMei','stu3':'XiaoMing'} print(Info.values()) # =>dict_values(['LiLei', 'HanMeiMei', 'XiaoMing'])
- pop:pop(self,k,d=None)删除指定的key,如果key存在,则返回对应的value,如果key不存在,则返回指定的d,如果d未指定,则报错
-
Info = {'stu1':'LiLei','stu2':'HanMeiMei','stu3':'XiaoMing'} print(Info.pop('stu1'))# =>LiLei print(Info.pop('stu4',0))# =>0 print(Info.pop('stu4'))# =>报错
- popitem:删除字典中的一对key-value,无序的,当字典为空时,报错
-
Info = {'stu1':'LiLei','stu2':'HanMeiMei','stu3':'XiaoMing'} print(Info.popitem())# =>('stu3', 'XiaoMing') print(Info.popitem())# =>('stu2', 'HanMeiMei') print(Info.popitem())# =>('stu1', 'LiLei') print(Info.popitem())# =>报错
- setdefault:setdefault(self,k,d=None)如果key存在,则返回对应的value,如果key不存在,则将该key-value添加进字典,并返回该value(即指定的d),若d未指定,则为None
-
Info = {'stu1':'LiLei','stu2':'HanMeiMei','stu3':'XiaoMing'} print(Info.setdefault('stu3','Jack'))# =>XiaoMing print(Info)# =>{'stu1': 'LiLei', 'stu2': 'HanMeiMei', 'stu3': 'XiaoMing'} print(Info.setdefault('stu4','Jack'))# =>Jack print(Info)# =>{'stu1': 'LiLei', 'stu2': 'HanMeiMei', 'stu3': 'XiaoMing', 'stu4': 'Jack'}
-
update:更新字典,若key不重复,则类似增添新的key-value;若key重复,则key对应的value会被覆盖
-
Info = {'stu1':'LiLei','stu2':'HanMeiMei','stu3':'XiaoMing'} Info_new = {'stu4':'Jack','stu5':'Tony','stu6':'Tom'} Info.update(Info_new) print(Info)# =>{'stu1': 'LiLei', 'stu2': 'HanMeiMei', 'stu3': 'XiaoMing', 'stu4': 'Jack', 'stu5': 'Tony', 'stu6': 'Tom'}
Info = {'stu1':'LiLei','stu2':'HanMeiMei','stu3':'XiaoMing'} Info_new = {'stu1':'Jack','stu5':'Tony','stu6':'Tom'} Info.update(Info_new) print(Info)# =>{'stu1': 'Jack', 'stu2': 'HanMeiMei', 'stu3': 'XiaoMing', 'stu5': 'Tony', 'stu6': 'Tom'}
- 其他语法
- 循环输出所有的key
-
Info = {'stu1':'LiLei','stu2':'HanMeiMei','stu3':'XiaoMing'} for key in Info: print(key)
-
Info = {'stu1':'LiLei','stu2':'HanMeiMei','stu3':'XiaoMing'} for key in Info.keys(): print(key)
- 循环输出所有的value
-
Info = {'stu1':'LiLei','stu2':'HanMeiMei','stu3':'XiaoMing'} for value in Info.values(): print(value)
- 循环输出所有的key-value
-
Info = {'stu1':'LiLei','stu2':'HanMeiMei','stu3':'XiaoMing'} for key in Info: print(key,Info[key])
-
del:删除指定的key,若key不存在,则报错
-
Info = {'stu1':'LiLei','stu2':'HanMeiMei','stu3':'XiaoMing'} del Info['stu1'] print(Info)# =>{'stu2': 'HanMeiMei', 'stu3': 'XiaoMing'} del Info['stu4']# =>报错