初识字典
标准类型 |
数字 |
字符串 |
列表 |
字典 |
元组 |
字典是Python语言中唯一的映射类型。
定义:{key1:value1,key2:value2}
1、键与值用冒号“:”分开;
2、项与项用逗号“,”分开;
特性:
1.key-value结构
2.key必须可hash、且必须为不可变数据类型、必须唯一
3.可存放任意多个值、可修改、可以不唯一
4.无序
字典的创建
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默认为None
注意:
>>> dic={}.fromkeys(['k1','k2'],[]) >>> dic {'k1': [], 'k2': []} >>> dic['k1'].append(1) >>> dic {'k1': [1], 'k2': [1]}
字典的常用操作
键、值、键值对
1、dic.keys() 返回一个包含字典所有KEY的列表;
2、dic.values() 返回一个包含字典所有value的列表;
3、dic.items() 返回一个包含所有(键,值)元祖的列表;
4、dic.iteritems()、dic.iterkeys()、dic.itervalues() 与它们对应的非迭代方法一样,不同的是它们返回一个迭代子,而不是一个列表;
新增
1、dic['new_key'] = 'new_value'
2、dic.setdefault(key, None) ,如果字典中不存在Key键,由 dic[key] = default 为它赋值;
删除
1、dic.pop(key[,default]) 和get方法相似。如果字典中存在key,删除并返回key对应的vuale;如果key不存在,且没有给出default的值,则引发keyerror异常;
2、dic.clear() 删除字典中的所有项或元素;
修改
1、dic['key'] = 'new_value',如果key在字典中存在,'new_value'将会替代原来的value值。
2、dic.update(dic2) 将字典dic2的键值对添加到字典dic中。
查看
1、dic['key'],返回字典中key对应的值,若key不存在字典中,则报错;
2、adict.get(key, default = None) 返回字典中key对应的值,若key不存在字典中,则返回default的值(default默认为None);
循环
长度
字典的工厂函数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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<span style="color: #0000ff;">def</span> copy(self): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown; restored from __doc__</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> D.copy() -> a shallow copy of D </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span><span style="color: #000000;"> @staticmethod </span><span style="color: #008000;">#</span><span style="color: #008000;"> known case</span> <span style="color: #0000ff;">def</span> fromkeys(*args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Returns a new dict with keys from iterable and values equal to value. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> get(self, k, d=None): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown; restored from __doc__</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> items(self): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown; restored from __doc__</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> D.items() -> a set-like object providing a view on D's items </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> keys(self): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown; restored from __doc__</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> D.keys() -> a set-like object providing a view on D's keys </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> pop(self, k, d=None): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown; restored from __doc__</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> 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 </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> popitem(self): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown; restored from __doc__</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> D.popitem() -> (k, v), remove and return some (key, value) pair as a 2-tuple; but raise KeyError if D is empty. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> setdefault(self, k, d=None): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown; restored from __doc__</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> update(self, E=None, **F): <span style="color: #008000;">#</span><span style="color: #008000;"> known special case of dict.update</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> 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] </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> values(self): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown; restored from __doc__</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> D.values() -> an object providing a view on D's values </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__contains__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> True if D has a key k, else False. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__delitem__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Delete self[key]. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__eq__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self==value. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__getattribute__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return getattr(self, name). </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__getitem__</span>(self, y): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown; restored from __doc__</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> x.__getitem__(y) <==> x[y] </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__ge__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self>=value. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__gt__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self>value. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__init__</span>(self, seq=None, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> known special case of dict.__init__</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> 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) </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__iter__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Implement iter(self). </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__len__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return len(self). </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__le__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self<=value. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__lt__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self<value. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span><span style="color: #000000;"> @staticmethod </span><span style="color: #008000;">#</span><span style="color: #008000;"> known case of __new__</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__new__</span>(*args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Create and return a new object. See help(type) for accurate signature. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__ne__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return self!=value. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__repr__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Return repr(self). </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__setitem__</span>(self, *args, **kwargs): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> Set self[key] to value. </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #0000ff;">def</span> <span style="color: #800080;">__sizeof__</span>(self): <span style="color: #008000;">#</span><span style="color: #008000;"> real signature unknown; restored from __doc__</span> <span style="color: #800000;">"""</span><span style="color: #800000;"> D.__sizeof__() -> size of D in memory, in bytes </span><span style="color: #800000;">"""</span> <span style="color: #0000ff;">pass</span> <span style="color: #800080;">__hash__</span> = None</pre>