dict() 方法
1.dict.clear 清除字典元素
dic ={1:"dd", 2:"aa"} dic.clear() print(dic)
2.dict.copy #浅拷贝dictionary
dic ={1:"dd", 2:"aa"} b=dic.copy() print(b)
3.dict.fromkeys #返回一个新的dictionary,key由iterable的元素组成,value等于value
a = dict.fromkeys(['k1','k2','k3'],'vvv') print(a)
4.dict.get#返回dictionary中key为指定值k对应的value,
dic ={1:"dd", 2:"aa"} print(dic.get(1))
5.dict.items #返回dictionary所有key-value对组成的集合
dic ={1:"dd", 2:"aa"} print(dic.items())
6.dict.keys
dic ={1:"dd", 2:"aa"}
print(dic.keys())
D:pythonpython.exe D:/py1/day1/str.py dict_keys([1, 2])
7.dict.pop #从dictionary中删除指定key,返回指定key对应的value。如果dictionary中不存在指定key,如果指定了d,返回d,否则抛出例外
dic ={1:"dd", 2:"aa"} dic.pop(1)
8.dict.popitem dict.popitem #删除并返回key-value对(key, value)作为2-tuple,如果dictionary为空,抛出例外
dic ={1:"dd", 2:"aa"}
print(dic.popitem())
D:pythonpython.exe D:/py1/day1/str.py
(1, 'dd')
9..setdefault #如果dictionary中不存在k,设置D[k]=d
>>> c {'k2': 'vvv', 'k1': 'vvv'} >>> c.setdefault('k1','v1') 'vvv' >>> c {'k2': 'vvv', 'k1': 'vvv'} >>> c.setdefault('k4','v4') 'v4' >>> c {'k2': 'vvv', 'k1': 'vvv', 'k4': 'v4'} >>>
10.dict.update
dict.update #使用E(dict/iterable)和F的数据更新dicti >>> a = {'ak1':'av1'} >>> b = {'bk1':'bv1'} >>> b.update(a) >>> b {'ak1': 'av1', 'bk1': 'bv1'}
11.dict.values
dict.values #返回dictionary所有value组成的集合 >>> b.values()