1. 字典是无序的,每次print()都不一样;
字典的键必须都是独一无二的,字典不能作为字典的key;
可以通过索引方式查到指定元素;
不能切片,因为是无序的;
2. dict()举例
dict1 = {'K1':18,2:True,'K3':[1,[],(),2,3,{'k1':'v1','kk3':(11,22)}],'K4':(11,22,33,44)} v = dict1['K3'][5]['kk3'][0] print(v) ———————————————————— 11
del dict1['K3'][5]['k1'] print(dict1) ———————————————— {'K1': 18, 2: True, 'K3': [1, [], (), 2, 3, {'kk3': (11, 22)}], 'K4': (11, 22, 33, 44)}
3.for 循环 keys()
dict1 = {'K1':18,2:True,'K3':[1,[],(),2,3,{'k1':'v1','kk3':(11,22)}],'K4':(11,22,33,44)} for item in dict1:#=dict1.keys() print(item) —————————————— K1 2 K3 K4
4. values()
for item in dict1.values(): print(item) ______________ 18 True [1, [], (), 2, 3, {'k1': 'v1', 'kk3': (11, 22)}] (11, 22, 33, 44)
5. items()
for k,v in dict1.items(): print(k,v) _____________ K1 18 2 True K3 [1, [], (), 2, 3, {'k1': 'v1', 'kk3': (11, 22)}] K4 (11, 22, 33, 44)
6. update()
dict1 = {'K1':18,2:True,'K3':[1,[],(),2,3,{'k1':'v1','kk3':(11,22)}],'K4':(11,22,33,44)} dict1.update(K1=111,K3=123) print(dict1) _______________________ {'K1': 111, 2: True, 'K3': 123, 'K4': (11, 22, 33, 44)}