初識字典
標準類型 |
數字 |
字符串 |
列表 |
字典 |
元組 |
字典是Python語言中惟一的映射類型。app
定義:{key1:value1,key2:value2}ide
一、鍵與值用冒號「:」分開;
二、項與項用逗號「,」分開;
特性:函數
1.key-value結構post
2.key必須可hash、且必須爲不可變數據類型、必須惟一this
3.可存聽任意多個值、可修改、能夠不惟一spa
4.無序rest
字典的建立
person = {"name": "alex", 'age': 20}
或
person = dict(name='seven', age=20)
person = dict({"name": "egon", 'age': 20})
person = dict((['name','苑昊'],['文周',18]))
{}.fromkeys(seq,100) #不指定100默認爲Nonecode
注意:blog
>>> dic={}.fromkeys(['k1','k2'],[]) >>> dic {'k1': [], 'k2': []} >>> dic['k1'].append(1) >>> dic {'k1': [1], 'k2': [1]}
字典的經常使用操做
鍵、值、鍵值對ci
一、dic.keys() 返回一個包含字典全部KEY的列表;
二、dic.values() 返回一個包含字典全部value的列表;
三、dic.items() 返回一個包含全部(鍵,值)元祖的列表;
四、dic.iteritems()、dic.iterkeys()、dic.itervalues() 與它們對應的非迭代方法同樣,不一樣的是它們返回一個迭代子,而不是一個列表;
新增
一、dic['new_key'] = 'new_value'
二、dic.setdefault(key, None) ,若是字典中不存在Key鍵,由 dic[key] = default 爲它賦值;
刪除
一、dic.pop(key[,default]) 和get方法類似。若是字典中存在key,刪除並返回key對應的vuale;若是key不存在,且沒有給出default的值,則引起keyerror異常;
2、dic.clear() 刪除字典中的全部項或元素;
修改
一、dic['key'] = 'new_value',若是key在字典中存在,'new_value'將會替代原來的value值。
二、dic.update(dic2) 將字典dic2的鍵值對添加到字典dic中。
查看
一、dic['key'],返回字典中key對應的值,若key不存在字典中,則報錯;
2、adict.get(key, default = None) 返回字典中key對應的值,若key不存在字典中,則返回default的值(default默認爲None);
循環
長度
字典的工廠函數
class dict(object): """ 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) """ def clear(self): # real signature unknown; restored from __doc__ """ D.clear() -> None. Remove all items from D. """ pass def copy(self): # real signature unknown; restored from __doc__ """ D.copy() -> a shallow copy of D """ pass @staticmethod # known case def fromkeys(*args, **kwargs): # real signature unknown """ Returns a new dict with keys from iterable and values equal to value. """ pass def get(self, k, d=None): # real signature unknown; restored from __doc__ """ D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. """ pass def items(self): # real signature unknown; restored from __doc__ """ D.items() -> a set-like object providing a view on D's items """ pass def keys(self): # real signature unknown; restored from __doc__ """ D.keys() -> a set-like object providing a view on D's keys """ pass def pop(self, k, d=None): # real signature unknown; restored from __doc__ """ 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 """ pass def popitem(self): # real signature unknown; restored from __doc__ """ D.popitem() -> (k, v), remove and return some (key, value) pair as a 2-tuple; but raise KeyError if D is empty. """ pass def setdefault(self, k, d=None): # real signature unknown; restored from __doc__ """ D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D """ pass def update(self, E=None, **F): # known special case of dict.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] """ pass 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 __hash__ = None