标签:pre run for div default cal class lin 必须
字典是另一种可变容器模型,且可存储任意类型对象。
字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中
d = {key1 : value1, key2 : value2 }
键必须是唯一的,但值则不必。
值可以取任何数据类型,但键必须是不可变的,如字符串,数字或元组。
键是不可变的,所以键只能用不可变类型的数据。
不可变类型: 整型,浮点型,字符串,元组
可变类型:列表,字典
一个简单的字典实例:
1 a=10 2 print(id(a)) 3 b=a 4 print(id(b)) 5 b=15 6 7 print(id(b)) 8 9 10 dic={‘name‘:‘Tom‘,‘age‘:‘21‘,‘gender‘:‘man‘,‘hobby‘:True} 11 print(dic)
结果:
1558212352 1558212352 1558212512 {‘name‘: ‘Tom‘, ‘age‘: ‘21‘, ‘gender‘: ‘man‘, ‘hobby‘: True}
新建字典对象
1 dic={‘name‘:‘Tom‘,‘age‘:‘21‘,‘gender‘:‘man‘,‘hobby‘:True} 2 3 a=list() 4 b_sub=[(‘name‘,‘Tom‘),(‘age‘,78)] 5 b=dict(b_sub) 6 7 print(type(b)) 8 print(b) 9 print(type(b_sub))
输出结果
<class ‘dict‘> {‘name‘: ‘Tom‘, ‘age‘: 78} <class ‘list‘>
字典的内置操作:
dic1={‘name‘:‘Tom‘} dic1[‘age‘]=18 print(dic1) ‘‘‘ radiansdict.setdefault(key, default=None) 和get()类似, 但如果键不存在于字典中,将会添加键并将值设为default ‘‘‘ age_1=dic1.setdefault(‘age‘,34) age_2=dic1.setdefault(‘hobby‘,‘running‘) print(dic1) print(age_1) print(age_2)
结果:
{‘name‘: ‘Tom‘, ‘age‘: 18} {‘name‘: ‘Tom‘, ‘age‘: 18, ‘hobby‘: ‘running‘} 18 running
操作:
dic1={‘name‘:‘Tom‘} dic1[‘age‘]=18 age_1=dic1.setdefault(‘age‘,34) age_2=dic1.setdefault(‘hobby‘,‘running‘) print(dic1) print(dic1.keys())#以列表返回一个字典所有的键 print(dic1.values())#以列表返回一个字典所有的值 print(dic1.items()) #以列表返回可遍历的(键, 值) 元组数组
结果:
{‘name‘: ‘Tom‘, ‘age‘: 18, ‘hobby‘: ‘running‘} dict_keys([‘name‘, ‘age‘, ‘hobby‘]) dict_values([‘Tom‘, 18, ‘running‘]) dict_items([(‘name‘, ‘Tom‘), (‘age‘, 18), (‘hobby‘, ‘running‘)])
能删单一的元素也能清空字典,清空只需一项操作。
显示删除一个字典用del命令
>>> dic1={‘name‘: ‘Tom‘, ‘age‘: 18, ‘hobby‘: ‘running‘} >>> dic2=dic1.copy() >>> dic2 {‘name‘: ‘Tom‘, ‘age‘: 18, ‘hobby‘: ‘running‘} >>> del dic2[‘age‘] >>> dic2 {‘name‘: ‘Tom‘, ‘hobby‘: ‘running‘} >>> dic2.clear() >>> dic2 {} >>> del dic2 >>> dic2 Traceback (most recent call last): File "<pyshell#11>", line 1, in <module> dic2 NameError: name ‘dic2‘ is not defined >>>
>>> dic3 {‘gender‘: ‘man‘, ‘city‘: ‘jiangsu‘} >>> dic1.update(dic3) >>> dic1 {‘name‘: ‘Tom‘, ‘age‘: 18, ‘hobby‘: ‘running‘, ‘gender‘: ‘man‘, ‘city‘: ‘jiangsu‘} >>> dic3 {‘gender‘: ‘man‘, ‘city‘: ‘jiangsu‘} >>> dic1.pop(‘city‘) ‘jiangsu‘ >>> dic1 {‘name‘: ‘Tom‘, ‘age‘: 18, ‘hobby‘: ‘running‘, ‘gender‘: ‘man‘} >>> dic1.pop(‘city‘) Traceback (most recent call last): File "<pyshell#23>", line 1, in <module> dic1.pop(‘city‘) KeyError: ‘city‘ >>> dic1.setdefault(‘city‘,‘changzhou‘) ‘changzhou‘ >>> dic1.pop(‘city‘) ‘changzhou‘ >>> dic1 {‘name‘: ‘Tom‘, ‘age‘: 18, ‘hobby‘: ‘running‘, ‘gender‘: ‘man‘} >>> dic1.setdefault(‘city‘,‘changzhou‘) ‘changzhou‘ >>> dic1 {‘name‘: ‘Tom‘, ‘age‘: 18, ‘hobby‘: ‘running‘, ‘gender‘: ‘man‘, ‘city‘: ‘changzhou‘} >>> dic1[‘city‘]=‘nanjing‘ >>> dic1 {‘name‘: ‘Tom‘, ‘age‘: 18, ‘hobby‘: ‘running‘, ‘gender‘: ‘man‘, ‘city‘: ‘nanjing‘} >>> dic1.pop(‘city‘) ‘nanjing‘ >>> dic1 {‘name‘: ‘Tom‘, ‘age‘: 18, ‘hobby‘: ‘running‘, ‘gender‘: ‘man‘} >>>
dic1.popitem()
>>> dic1 {‘name‘: ‘Tom‘, ‘age‘: 18, ‘hobby‘: ‘running‘, ‘gender‘: ‘man‘} >>> dic1.popitem() (‘gender‘, ‘man‘) >>> dic1.popitem() (‘hobby‘, ‘running‘) >>> dic1 {‘name‘: ‘Tom‘, ‘age‘: 18} >>>
>>> dic1 {‘name‘: ‘Tom‘, ‘age‘: 18} >>> dic4=dict.fromkeys(dic1) >>> dic4 {‘name‘: None, ‘age‘: None} >>> dic4[‘name‘]=‘Tom‘ >>> dic4 {‘name‘: ‘Tom‘, ‘age‘: None} >>> dic5=dict.fromkeys(dic1,20) >>> dic5 {‘name‘: 20, ‘age‘: 20} >>>
1 dic1={‘name‘: ‘Tom‘, ‘age‘: 18, ‘hobby‘: ‘running‘} 2 dic2=dict.fromkeys(dic1,‘demo‘) 3 4 print(dic1) 5 print(dic2) 6 7 dic3=dict.fromkeys(dic1,[‘demo‘,‘test‘]) 8 print(dic3)
结果:
{‘name‘: ‘Tom‘, ‘age‘: 18, ‘hobby‘: ‘running‘} {‘name‘: ‘demo‘, ‘age‘: ‘demo‘, ‘hobby‘: ‘demo‘} {‘name‘: [‘demo‘, ‘test‘], ‘age‘: [‘demo‘, ‘test‘], ‘hobby‘: [‘demo‘, ‘test‘]}
1 dic1={‘name‘: ‘Tom‘, ‘age‘: 18, ‘hobby‘: ‘running‘} 2 dic2=dict.fromkeys(dic1,‘demo‘) 3 4 print(dic1) 5 print(dic2) 6 7 dic3=dict.fromkeys(dic1,[‘demo‘,‘test‘]) 8 print(dic3) 9 10 dic3[‘name‘][1]=‘test_change‘ 11 print(dic3)
{‘name‘: ‘Tom‘, ‘age‘: 18, ‘hobby‘: ‘running‘} {‘name‘: ‘demo‘, ‘age‘: ‘demo‘, ‘hobby‘: ‘demo‘} {‘name‘: [‘demo‘, ‘test‘], ‘age‘: [‘demo‘, ‘test‘], ‘hobby‘: [‘demo‘, ‘test‘]} {‘name‘: [‘demo‘, ‘test_change‘], ‘age‘: [‘demo‘, ‘test_change‘], ‘hobby‘: [‘demo‘, ‘test_change‘]}
遍历字典:
1 dic={4:‘a‘,6:‘b‘,3:‘c‘,9:‘d‘} 2 3 print(sorted(dic.items())) 4 print(sorted(dic.keys())) 5 print(sorted(dic.values())) 6 print(sorted(dic)) 7 8 print(‘-----------遍历字典1-------------‘) 9 for i in dic: 10 print(i,dic[i]) 11 12 13 print(‘-----------遍历字典2-------------‘) 14 for i in dic.items(): 15 print(i) 16 17 print(‘-----------遍历字典3-------------‘) 18 for i in dic.keys(): 19 print(i) 20 21 print(‘-----------遍历字典4-------------‘) 22 for i in dic.values(): 23 print(i)
结果:
[(3, ‘c‘), (4, ‘a‘), (6, ‘b‘), (9, ‘d‘)] [3, 4, 6, 9] [‘a‘, ‘b‘, ‘c‘, ‘d‘] [3, 4, 6, 9] -----------遍历字典1------------- 4 a 6 b 3 c 9 d -----------遍历字典2------------- (4, ‘a‘) (6, ‘b‘) (3, ‘c‘) (9, ‘d‘) -----------遍历字典3------------- 4 6 3 9 -----------遍历字典4------------- a b c d
标签:pre run for div default cal class lin 必须
原文地址:https://www.cnblogs.com/Mengchangxin/p/9144162.html