字典的方法

>>> for i in dir(dict):print(i)

clear 清空字典

copy 浅拷贝

fromkeys创建一个新字典,并初始化

get 返回键对应的值,可以设置没有指定键时的默认值

items以迭代器返回可遍历的(键, 值) 元组数组

keys以迭代器返回字典的键

pop删除字典中指定的键和对应的值

popitem删除字典最后的键值对

setdefault查找键的值,如果键不存在添加键并设置默认值

update把字典参数dict2的key/value(键/值)对更新到字典dict里

values 以迭代器返回字典的值

clear 清空字典

>>> help(dict.clear)
Help on method_descriptor:
clear(...)
D.clear() -> None. Remove all items from D.

示例:

>>> D = {'name':'hh','age':18}
>>> D
{'name': 'hh', 'age': 18}
>>> D.clear()
>>> D
{}

copy 浅拷贝

>>> help(dict.copy)
Help on method_descriptor:
copy(...)
D.copy() -> a shallow copy of D

示例:

1、dict.copy()浅拷贝

拷贝父对象,不会拷贝父对象内部的子对象。即两个对象的子对象指向同一个引用

字典 单词识别首字母 python python字典搜索_python字典创建查找

>>> D = {'k1':['a','b'],'k2':{'k21':'x'}}
>>> D1 = D.copy()
>>> D
{'k1': ['a', 'b'], 'k2': {'k21': 'x'}}
>>> D1
{'k1': ['a', 'b'], 'k2': {'k21': 'x'}}

#下面可以看出,子对象指向同一个引用,原位置修改任意对象的子对象,两个对象的子对象都会修改,但赋值新的引用不会影响另外一个

>> id(D)
50215024
>>> id(D1)
49690304
>>> id(D['k1'])
50105672
>>> id(D1['k1'])
50105672
>>> D['k1'][0]=1 #修改父对象的子对象,原位置修改
>>> D
{'k1': [1, 'b'], 'k2': {'k21': 'x'}}
>>> D1
{'k1': [1, 'b'], 'k2': {'k21': 'x'}}
>>> D1['k2']['k21']='y' #修改子对象的子对象,原位置修改
>>> D
{'k1': [1, 'b'], 'k2': {'k21': 'y'}}
>>> D1
{'k1': [1, 'b'], 'k2': {'k21': 'y'}}
>>> D['k1']=1 #修改父对象的子对象,重新引用
>>> D
{'k1': 1, 'k2': {'k21': 'y'}}
>>> D1
{'k1': [1, 'b'], 'k2': {'k21': 'y'}}

2、copy.deepcopy()深拷贝

深拷贝是对原有对象的完全拷贝,同时拷贝父对象和子对象

字典 单词识别首字母 python python字典搜索_python字典创建查找_02

>>> import copy
>>> D = {'k1':['a','b'],'k2':{'k21':'x'}}
>>> D1 = copy.deepcopy(D)
>>> D
{'k1': ['a', 'b'], 'k2': {'k21': 'x'}}
>>> D1
{'k1': ['a', 'b'], 'k2': {'k21': 'x'}}

#下面可以看出,深拷贝后,两个对象完全不相干

>>> id(D)
50214808
>>> id(D1)
50214736
>>> id(D['k1'])
50076872
>>> id(D1['k1'])
49835016

#完全不相干,原位置修改子对象不会影响另外一个

>>> D['k1'][0]=1
>>> D
{'k1': [1, 'b'], 'k2': {'k21': 'x'}}
>>> D1
{'k1': ['a', 'b'], 'k2': {'k21': 'x'}}

3、等号赋值

完全是同一个引用

字典 单词识别首字母 python python字典搜索_迭代器_03

>>> D = {'k1':['a','b'],'k2':{'k21':'x'}}
>>> D1=D
>>> id(D)
49690304
>>> id(D1)
49690304

fromkeys 创建一个新字典,并初始化

创建一个新字典,以序列seq中元素做字典的键,value为字典所有键对应的初始值。

>>> help(dict.fromkeys)
Help on built-in function fromkeys:
fromkeys(iterable, value=None, /) method of builtins.type instance
Returns a new dict with keys from iterable and values equal to value.

示例:

>>> D = dict.fromkeys(['k1','k2','k3'])
>>> D
{'k1': None, 'k2': None, 'k3': None}
>>> D = dict.fromkeys(['k1','k2','k3'],'v')
>>> D
{'k1': 'v', 'k2': 'v', 'k3': 'v'}
>>> D = dict.fromkeys(['k1','k2','k3'],[1,2,3])
>>> D
{'k1': [1, 2, 3], 'k2': [1, 2, 3], 'k3': [1, 2, 3]}

get 返回键对应的值,可以设置没有指定键时的默认值

没有指定的键返回None,可以设置没有指定的键时的返回值,默认为None

>>> help(dict.get)
Help on method_descriptor:
get(...)
D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.
>>> d = {'bob':11,'mary':12}
>>> d.get('bob')
11
>>> d.get('shery')
>>> bool(d.get('shery'))
False
>>> d.get('shery','100')
'100'

items 以迭代器返回可遍历的(键, 值) 元组数组

>>> help(dict.items)
Help on method_descriptor:
items(...)
D.items() -> a set-like object providing a view on D's items

示例:

>>> D = {'k1':'v1','k2':'v2'}
>>> D.items()
dict_items([('k1', 'v1'), ('k2', 'v2')])
>>> for i in D.items():print(i)
('k1', 'v1')
('k2', 'v2')

keys以迭代器返回字典的键

>>> help(dict.keys)
Help on method_descriptor:
keys(...)
D.keys() -> a set-like object providing a view on D's keys

示例:

>>> D = {'k1':'v1','k2':'v2'}
>>> D.keys()
dict_keys(['k1', 'k2'])
>>> for i in D.keys():print(i)
k1
k2

pop 删除字典中指定的键和对应的值

删除字典给定键key所对应的值,返回值为被删除的值。

没有找到key值返回default值,如果没有指定default,报错。

>>> help(dict.pop)
Help on method_descriptor:
pop(...)
D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
If key is not found, d is returned if given, otherwise KeyError is raised

示例

>>> D={'k1':1,'k2':[1,2]}
>>> D.pop('k1')
1
>>> D
{'k2': [1, 2]}
>>> D.pop('k3')
Traceback (most recent call last):
File "", line 1, in 
D.pop('k3')
KeyError: 'k3'
>>> D.pop('k3','noexist')
'noexist'

popitem 删除字典最后的键值对

如果字典为空会报错

>>> help(dict.popitem)
Help on method_descriptor:
popitem(...)
D.popitem() -> (k, v), remove and return some (key, value) pair as a
2-tuple; but raise KeyError if D is empty.
>>> D={'k1':1,'k2':[1,2]}
>>> D.popitem()
('k2', [1, 2])
>>> D.popitem()
('k1', 1)
>>>
>>>
>>> D.popitem()
Traceback (most recent call last):
File "", line 1, in 
D.popitem()
KeyError: 'popitem(): dictionary is empty'

setdefault 查找键的值,如果键不存在添加键并设置默认值

>>> help(dict.setdefault)
Help on method_descriptor:
setdefault(...)
D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D

示例

>>> D={'k1':1,'k2':[1,2]}
>>> D.setdefault('k1')
1
>>> D.setdefault('k2')
[1, 2]
>>> D.setdefault('k3')
>>> D
{'k1': 1, 'k2': [1, 2], 'k3': None}
>>> D.setdefault('k4','x')
'x'
>>> D
{'k1': 1, 'k2': [1, 2], 'k3': None, 'k4': 'x'}

update把字典参数 dict2 的 key/value(键/值) 对更新到字典 dict 里

如果有重复的键,值会覆盖掉原来的

>>> help(dict.update)
Help on method_descriptor:
update(...)
D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]
If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v
In either case, this is followed by: for k in F: D[k] = F[k]

示例:

>>> D={'k1':1,'k2':[1,2]}
>>> D1 = {'k3':'x'}
>>> D.update(D1)
>>> D
{'k1': 1, 'k2': [1, 2], 'k3': 'x'}
>>> D2 = {'k3':'y'}
>>> D.update(D2)
>>> D
{'k1': 1, 'k2': [1, 2], 'k3': 'y'}

values以迭代器返回字典的值

>>> help(dict.values)
Help on method_descriptor:
values(...)
D.values() -> an object providing a view on D's values

示例:

>>> D = {'k1':'v1','k2':'v2'}
>>> D.values()
dict_values(['v1', 'v2'])
>>> for i in D.values():print(i)
v1
v2