字典一种key - value 的数据类型。
字典是无序的,键必须是唯一的,但值则不必,值可以取任何数据类型,但键必须是不可变的,如字符串,数字或元组。
字典的常用操作
创建
直接创建
dict1 = {‘student1‘:‘01‘,‘color‘:[‘red‘,‘white‘],‘guangdong‘:{‘guangzhou‘:[‘haizhu‘,‘tianhe‘],‘shenzhen‘:[‘nanshan‘,‘futian‘]}}
通过dict函数创建
#通过list创建 >>> student = [(‘student1‘,‘01‘),(‘student2‘,‘02‘)] >>> student_dict = dict(student) >>> student_dict {‘student1‘: ‘01‘, ‘student2‘: ‘02‘} #通过关键字参数创建 >>> student_dict = dict(student1=‘01‘,student2=‘02‘,student3=‘03‘) >>> student_dict {‘student1‘: ‘01‘, ‘student2‘: ‘02‘, ‘student3‘: ‘03‘}
追加
>>> student_dict {‘student1‘: ‘01‘, ‘student2‘: ‘02‘, ‘student3‘: ‘03‘} >>> student_dict[‘student4‘] = ‘04‘ >>> student_dict {‘student1‘: ‘01‘, ‘student2‘: ‘02‘, ‘student3‘: ‘03‘, ‘student4‘: ‘04‘}
修改
>>> student_dict {‘student1‘: ‘01‘, ‘student2‘: ‘02‘, ‘student3‘: ‘03‘, ‘student4‘: ‘04‘} >>> student_dict[‘student4‘] = ‘004‘ >>> student_dict {‘student1‘: ‘01‘, ‘student2‘: ‘02‘, ‘student3‘: ‘03‘, ‘student4‘: ‘004‘}
删除
>>> student_dict {‘student1‘: ‘01‘, ‘student2‘: ‘02‘, ‘student3‘: ‘03‘, ‘student4‘: ‘004‘} >>> student_dict.pop(‘student4‘) ‘004‘ >>> student_dict {‘student1‘: ‘01‘, ‘student2‘: ‘02‘, ‘student3‘: ‘03‘} >>> del student_dict[‘student3‘] >>> student_dict {‘student1‘: ‘01‘, ‘student2‘: ‘02‘} >>> student_dict.popitem() #随机删除 (‘student2‘, ‘02‘) >>> student_dict {‘student1‘: ‘01‘}
查找
>>> student_dict {‘student1‘: ‘01‘, ‘student2‘: ‘02‘, ‘student3‘: ‘03‘, ‘student4‘: ‘04‘} >>> ‘student1‘ in student_dict True >>> student_dict.get(‘student1‘) ‘01‘ >>> student_dict[‘student1‘] ‘01‘ >>> student_dict.get(‘student5‘) >>> student_dict[‘student5‘] #如果一个key不存在,就报错,get方法不会,不存在只返回None
>>> student_dict {‘student1‘: ‘01‘, ‘student2‘: ‘02‘, ‘student3‘: ‘03‘, ‘student4‘: ‘04‘} >>> student_dict.keys() dict_keys([‘student1‘, ‘student2‘, ‘student3‘, ‘student4‘]) >>> student_dict.values() dict_values([‘01‘, ‘02‘, ‘03‘, ‘04‘]) >>> student_dict.items() dict_items([(‘student1‘, ‘01‘), (‘student2‘, ‘02‘), (‘student3‘, ‘03‘), (‘student4‘, ‘04‘)]) >>> type(student_dict.items()) <class ‘dict_items‘> >>> type(student_dict) <class ‘dict‘>
#嵌套字典查找 >>> dict1 = {‘student1‘:‘01‘,‘color‘:[‘red‘,‘white‘],‘guangdong‘:{‘guangzhou‘:[‘haizhu‘,‘tianhe‘],‘shenzhen‘:[‘nanshan‘,‘futian‘]}} >>> dict1[‘guangdong‘][‘guangzhou‘][0] ‘haizhu‘
更新
>>> student_dict2 = {‘student5‘:‘05‘,‘student2‘:‘002‘} >>> student_dict {‘student1‘: ‘01‘, ‘student2‘: ‘02‘, ‘student3‘: ‘03‘, ‘student4‘: ‘04‘} >>> student_dict.update(student_dict2) #用字典student_dict2更新,更新键对应的新值,添加新的键值对。 >>> student_dict {‘student1‘: ‘01‘, ‘student2‘: ‘002‘, ‘student3‘: ‘03‘, ‘student4‘: ‘04‘, ‘student5‘: ‘05‘}
遍历
#方法1 for key in info: print(key,info[key]) #方法2 for k,v in info.items(): #会先把dict转成list,数据大时尽量不要用 print(k,v)
字典相关的函数
序号 | 函数及描述 | 实例 |
---|---|---|
1 | len(dict) 计算字典元素个数,即键的总数。 |
>>> dict = {‘Name‘: ‘Runoob‘, ‘Age‘: 7, ‘Class‘: ‘First‘}
>>> len(dict)
3
|
2 | str(dict) 输出字典,以可打印的字符串表示。 |
>>> dict = {‘Name‘: ‘Runoob‘, ‘Age‘: 7, ‘Class‘: ‘First‘}
>>> str(dict)
"{‘Name‘: ‘Runoob‘, ‘Class‘: ‘First‘, ‘Age‘: 7}"
|
3 | type(variable) 返回输入的变量类型,如果变量是字典就返回字典类型。 |
>>> dict = {‘Name‘: ‘Runoob‘, ‘Age‘: 7, ‘Class‘: ‘First‘}
>>> type(dict)
<class ‘dict‘>
|
字典的内置方法
序号 | 函数及描述 |
---|---|
1 | radiansdict.clear() 删除字典内所有元素 |
2 | radiansdict.copy() 返回一个字典的浅复制 |
3 | radiansdict.fromkeys() 创建一个新字典,以序列seq中元素做字典的键,val为字典所有键对应的初始值 |
4 | radiansdict.get(key, default=None) 返回指定键的值,如果值不在字典中返回default值 |
5 | key in dict 如果键在字典dict里返回true,否则返回false |
6 | radiansdict.items() 以列表返回可遍历的(键, 值) 元组数组 |
7 | radiansdict.keys() 以列表返回一个字典所有的键 |
8 | radiansdict.setdefault(key, default=None) 和get()类似, 但如果键不存在于字典中,将会添加键并将值设为default |
9 | radiansdict.update(dict2) 把字典dict2的键/值对更新到dict里 |
10 | radiansdict.values() 以列表返回字典中的所有值 |
11 | pop(key[,default]) 删除字典给定键 key 所对应的值,返回值为被删除的值。key值必须给出。 否则,返回default值。 |
12 | popitem() 随机返回并删除字典中的一对键和值(一般删除末尾对)。 |
参考链接:http://www.runoob.com/python3/python3-dictionary.html
参考链接:http://www.cnblogs.com/alex3714/articles/5717620.html