字典是一种映射类型。映射类型包括对象里的哈希值(键)和指向的对象(值)。
一、字典的创建
创建字典值需要把字典赋值给变量即可。
>>> dic1 = {'k1':'v1','k2':'v2'} >>> dic1 {'k2': 'v2', 'k1': 'v1'}
>>> dic2 = dict(k1='v1',k2='v2') >>> dic2 {'k2': 'v2', 'k1': 'v1'}
>>> dic3 = {}.fromkeys(('k1','k2'),-1) >>> dic3 {'k2': -1, 'k1': -1}
二、字典的一些操作
a.访问
要访问字典中的某个元素的值,可用字典加上中括号‘[]’来得到。
>>> dic2['k1'] 'v1'
b.更新
>>> dic2['k1'] = 'v3' >>> dic2 {'k2': 'v2', 'k1': 'v3'}
c.删除
>>> del dic2['k2'] >>> dic2 {'k1': 'v3'}
>>> dic2.clear() >>> dic2 {}
三、内置函数&方法
def clear(self): #删除字典中所有元素 def copy(self): #浅copy def fromkeys(*args, **kwargs): #创建并返回一个新字典 dic3 = {}.fromkeys(('k1','k2'),-1) >>> dic3 def get(self, k, d=None): #对字典dict 中的键key,返回它对应的值value def items(self): #获取一个包含字典中(键, 值)对元组的列表 def keys(self): #获取一个包含字典中键的列表 def pop(self, k, d=None): #和方法get()相似,如果字典中key 键存在,删除并返回dict[key],如果key 键不存在,且没有给出default 的值 def popitem(self): def setdefault(self, k, d=None): #字典中不存在key 键,由dict[key]=default 为它赋值 >>> dic2.setdefault('k4') >>> dic2 {'k4': None} def update(self, E=None, **F): #将字典的键-值对添加到字典dict >>> dic1 {'k2': 'v2', 'k1': 'v1'} >>> dic1.update({'k3':123}) >>> dic1 {'k2': 'v2', 'k3': 123, 'k1': 'v1'} def values(self): # real signature unknown; restored from __doc__ """ D.values() -> an object providing a view on D's values """ pass def __contains__(self, *args, **kwargs): # real signature unknown """ True if D has a key k, else False. """ pass def __delitem__(self, *args, **kwargs): # real signature unknown """ Delete self[key]. """ pass def __eq__(self, *args, **kwargs): # real signature unknown """ Return self==value. """ pass def __getattribute__(self, *args, **kwargs): # real signature unknown """ Return getattr(self, name). """ pass def __getitem__(self, y): # real signature unknown; restored from __doc__ """ x.__getitem__(y) <==> x[y] """ pass def __ge__(self, *args, **kwargs): # real signature unknown """ Return self>=value. """ pass def __gt__(self, *args, **kwargs): # real signature unknown """ Return self>value. """ pass def __init__(self, seq=None, **kwargs): # known special case of dict.__init__ """ dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2) # (copied from class doc) """ pass def __iter__(self, *args, **kwargs): # real signature unknown """ Implement iter(self). """ pass def __len__(self, *args, **kwargs): # real signature unknown """ Return len(self). """ pass def __le__(self, *args, **kwargs): # real signature unknown """ Return self<=value. """ pass def __lt__(self, *args, **kwargs): # real signature unknown """ Return self<value. """ pass @staticmethod # known case of __new__ def __new__(*args, **kwargs): # real signature unknown """ Create and return a new object. See help(type) for accurate signature. """ pass def __ne__(self, *args, **kwargs): # real signature unknown """ Return self!=value. """ pass def __repr__(self, *args, **kwargs): # real signature unknown """ Return repr(self). """ pass def __setitem__(self, *args, **kwargs): # real signature unknown """ Set self[key] to value. """ pass def __sizeof__(self): # real signature unknown; restored from __doc__ """ D.__sizeof__() -> size of D in memory, in bytes """ pass