字典的方法
1.查询&修改
person_dicts = { 'nancy':28,'mily':30,'maria':31,'jack':43,'guo':31 }
print(person_dicts['nancy']) #查询
person_dicts['nancy'] = '不清楚' #修改
print(person_dicts)
结果:
28
{'nancy': '不清楚', 'mily': 30, 'maria': 31, 'jack': 43, 'guo': 31}
2.删除
person_dicts = { 'nancy':28,'mily':30,'maria':31,'jack':43,'guo':31 }
del person_dicts['nancy'] #删除nancy
person_dicts.pop('mily') #删除mily
person_dicts.popitem() #随机删除
print(person_dicts)
结果:
{'maria': 31, 'jack': 43}
3.增加
person_dicts = { 'nancy':28,'mily':30,'maria':31,'jack':43,'guo':31 }
person_dicts['zero'] = 20 #增加
print(person_dicts)
结果:
{'nancy': 28, 'mily': 30, 'maria': 31, 'jack': 43, 'guo': 31, 'zero': 20}
4.get
访问不存在的项时,将引发错误。使用get访问不存在的key时,没有引发异常,而是返回none。如果存在指定的key,get的作用是查找。
person_dicts = { 'nancy':28,'mily':30,'maria':31,'jack':43,'guo':31 }
print(person_dicts.get('zero')) #访问不存在的key,返回none
print(person_dicts.get('nancy')) #访问存在的key
print(person_dicts)
结果:
None
28
{'nancy': 28, 'mily': 30, 'maria': 31, 'jack': 43, 'guo': 31}
5.setdefault
如果指定的key值存在,返回其值,保持不变;如果key不存在,则返回指定的值并更新字典,如果没有指定,默认为None。
person_dicts = { 'nancy':28,'mily':30,'maria':31,'jack':43,'guo':31 }
print(person_dicts.setdefault('zero')) #获取不存在的key值’zero‘,返回none
print(person_dicts) #查询是否添加了zero-none的值
print(person_dicts.setdefault('demo',100)) #获取不存在的key值’demo‘,并指定value
print(person_dicts)
print(person_dicts.setdefault('nancy',200)) #获取存在的key值,nancy
print(person_dicts) #nancy的值应该不变
结果:
None
{'nancy': 28, 'mily': 30, 'maria': 31, 'jack': 43, 'guo': 31, 'zero': None}
100
{'nancy': 28, 'mily': 30, 'maria': 31, 'jack': 43, 'guo': 31, 'zero': None, 'demo': 100}
28
{'nancy': 28, 'mily': 30, 'maria': 31, 'jack': 43, 'guo': 31, 'zero': None, 'demo': 100}
6.fromkeys
print(dict.fromkeys(['one','two','three'])) #创建一个新字典,value默认为None
print(dict.fromkeys(['one','two','three'],'unknown')) #创建一个新字典,value为unknown
结果:
{'one': None, 'two': None, 'three': None}
{'one': 'unknow', 'two': 'unknow', 'three': 'unknow'}
7.items
方法items返回一个包含所有字典项的列表,每个元素都是(key,value)的形式。
dict = { 'one':1,'two':2,'three':3,'four':4}
t = dict.items() #返回字典视图的特殊类型
print(t)
if ('one',1) in t:
print('True')
结果:
dict_items([(‘one’, 1), (‘two’, 2), (‘three’, 3), (‘four’, 4)])
True
8.keys & values
dict = { 'one':1,'two':2,'three':3,'four':4}
print(dict.keys()) #返回字典中的key值
print(dict.values()) #返回字典中的value值
结果:
dict_keys(['one', 'two', 'three', 'four'])
dict_values([1, 2, 3, 4])
9.update
update使用一个字典来更新另一个字典
dict = { 'one':1,'two':2,'three':3,'four':4}
people = {'jack':22,'tom':23,'helen':33,'one':111}
print(dict)
dict.update((people)) #用people来更新dict
print(dict)
结果:
{'one': 1, 'two': 2, 'three': 3, 'four': 4}
{'one': 111, 'two': 2, 'three': 3, 'four': 4, 'jack': 22, 'tom': 23, 'helen': 33}
10.打印字典
dict = { 'one':1,'two':2,'three':3,'four':4}
for i in dict:
print(i) #只打印字典中的key值
print('---------分隔线-------------')
for i in dict:
print(i,dict[i]) #打印字典
结果:
one
two
three
four
---------分隔线-------------
one 1
two 2
three 3
four 4
11.clear
dict = { 'one':1,'two':2,'three':3,'four':4}
dict.clear() #删除所有的字典项
print(dict)
12.copy 浅复制
dict = {'one':1,'two':2,'three':3,'four':4,'five':['bar','foo','baz']}
dict_copy = dict.copy()
dict_copy['one'] = 111 #改变副本中的值,原件不受影响
dict_copy['five'].remove('bar') #改变二层列表中的值,原件受到影响
print(dict)
print(dict_copy)
结果:
{'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': ['foo', 'baz']}
{'one': 111, 'two': 2, 'three': 3, 'four': 4, 'five': ['foo', 'baz']}
为了避免上面修改二层列表中的问题,可以执行深复制
from copy import deepcopy
dict = {'one':1,'two':2,'three':3,'four':4,'five':['bar','foo','baz']}
dict_copy = deepcopy(dict)
dict_copy['one'] = 111 #改变副本中的值,原件不受影响
dict_copy['five'].remove('bar') #改变二层列表中的值,原件受到影响
print(dict)
print(dict_copy)
结果:
{'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': ['bar', 'foo', 'baz']}
{'one': 111, 'two': 2, 'three': 3, 'four': 4, 'five': ['foo', 'baz']}