‘‘‘1.访问、修改,删除字典中的值:‘‘‘ dict={‘a‘:‘11‘,‘b‘:‘22‘,‘c‘:‘33‘,‘d‘:‘44‘}print dict[‘a‘],dict[‘d‘] #访问dict[‘b‘]=‘abc‘ #修改print dict#删除del dict[‘c‘] #删除字典中的某个值print dictdict.clear() #清空字典print dictdel dict #删除字典---------------------------------------------
11 44
{‘a‘: ‘11‘, ‘c‘: ‘33‘, ‘b‘: ‘abc‘, ‘d‘: ‘44‘}
{‘a‘: ‘11‘, ‘b‘: ‘abc‘, ‘d‘: ‘44‘}
{}
-----------------------------------------------------------------------------------
‘‘‘2.len():计算字典的元素总数,即键的总数‘‘‘dict={‘a‘:‘11‘,‘b‘:‘22‘,‘c‘:‘33‘,‘d‘:‘44‘}print len(dict)
---------------------------------------------
4
-----------------------------------------------------------------------------------
‘‘‘3.fromkeys(seq,value):函数用于创建一个新字典,以序列 seq 中元素做字典的键,value 为字典所有键对应的初始值。‘‘‘seq={‘a‘:‘11‘,‘b‘:‘22‘,‘c‘:‘33‘,‘d‘:‘44‘}dict=dict.fromkeys(seq)print dictprint dict.fromkeys(seq,20)
---------------------------------------------
{‘a‘: None, ‘c‘: None, ‘b‘: None, ‘d‘: None}
{‘a‘: 20, ‘c‘: 20, ‘b‘: 20, ‘d‘: 20}
-----------------------------------------------------------------------------------
‘‘‘4.get():返回指定键的值,如果值不在字典中,返回默认值 setdefault():函数和get() 方法类似, 如果键不存在于字典中,将会添加键并将值设为默认值。‘‘‘
seq={‘a‘:‘11‘,‘b‘:‘22‘,‘c‘:‘33‘,‘d‘:‘44‘}print seq.get(‘a‘)print seq.get(‘e‘)print seq.get(‘e‘,‘fuck‘)print seqprint seq.setdefault(‘e‘,‘fuck‘)print seq
---------------------------------------------
11
None
fuck
{‘a‘: ‘11‘, ‘c‘: ‘33‘, ‘b‘: ‘22‘, ‘d‘: ‘44‘}
fuck
{‘a‘: ‘11‘, ‘c‘: ‘33‘, ‘b‘: ‘22‘, ‘e‘: ‘fuck‘, ‘d‘: ‘44‘}
-----------------------------------------------------------------------------------
‘‘‘5.has_key():用于判断键是否存在于字典中,如果键在字典dict里返回true,否则返回false。‘‘‘seq={‘a‘:‘11‘,‘b‘:‘22‘,‘c‘:‘33‘,‘d‘:‘44‘}print seq.has_key(‘a‘)print seq.has_key(‘m‘)
---------------------------------------------
True
False
-----------------------------------------------------------------------------------
‘‘‘6.items():以列表返回可遍历的(键, 值) 元组数组‘‘‘seq={‘a‘:‘11‘,‘b‘:‘22‘,‘c‘:‘33‘,‘d‘:‘44‘}print seq.items()for k,v in seq.items(): print k,v
---------------------------------------------
[(‘a‘, ‘11‘), (‘c‘, ‘33‘), (‘b‘, ‘22‘), (‘d‘, ‘44‘)]
a 11
c 33
b 22
d 44
-----------------------------------------------------------------------------------
‘‘‘7.keys():以列表返回一个字典中所有的键‘‘‘seq={‘a‘:‘11‘,‘b‘:‘22‘,‘c‘:‘33‘,‘d‘:‘44‘}print seq.keys()
---------------------------------------------
[‘a‘, ‘c‘, ‘b‘, ‘d‘]
-----------------------------------------------------------------------------------
‘‘‘8.dict.update(dict2):将dict2中的键值更新到dict中‘‘‘dict={‘a‘:‘11‘,‘b‘:‘22‘,‘c‘:‘33‘,‘d‘:‘44‘}dict1={‘aaa‘:‘111‘,‘bbb‘:‘222‘}dict.update(dict1)print dict
---------------------------------------------
{‘a‘: ‘11‘, ‘c‘: ‘33‘, ‘b‘: ‘22‘, ‘aaa‘: ‘111‘, ‘d‘: ‘44‘, ‘bbb‘: ‘222‘}
-----------------------------------------------------------------------------------
‘‘‘9.values():以列表返回字典中所有的值‘‘‘dict={‘a‘:‘11‘,‘b‘:‘22‘,‘c‘:‘33‘,‘d‘:‘44‘}print dict.values()
---------------------------------------------
[‘11‘, ‘33‘, ‘22‘, ‘44‘]
-----------------------------------------------------------------------------------
‘‘‘10.pop(key[,default]):删除字典给定键 key 所对应的值,返回值为被删除的值。key值必须给出。 否则,返回default值。‘‘‘dict={‘a‘:‘11‘,‘b‘:‘22‘,‘c‘:‘33‘,‘d‘:‘44‘}print dict.pop(‘a‘)print dict.pop(‘e‘,‘notall‘)
---------------------------------------------
11
notall
-----------------------------------------------------------------------------------
原文地址:https://www.cnblogs.com/chushujin/p/9355824.html
时间: 2024-10-09 13:48:35