标签:返回值 ... false python对象 -- eth class div 容器
字典:一个字典对象是可变的,它是一个容器类型,能存储任意个数的python对象,包括列表、元组;
通过键直接“映射”到值,来存储数据,其是无序的;
键不可变,一般使用字符串作为键,一条字典条目的语法格式是键值,多条被包含在大括号里。
1、创建字典
1.1直接创建:
>>>dict1 = {} >>>dict2 = {"name":"earth","port":80} >>>dict1,dict2 ({},{"name":"earth","port":80})
1.2工厂方法dict()
>>>fdict = dict(([‘x‘,1],[‘y‘,2])) >>>fdict {‘y‘: 2, ‘x‘: 1}
1.3快速创建
>>>ddict = {}.fromkeys((‘x‘,‘y‘,‘z‘),-1)#指定值 >>>ddict {‘y‘: -1, ‘x‘: -1, ‘z‘: -1} >>>edict = {}.fromkeys((‘x‘,‘y‘,‘z‘))#默认值为None >>>edict {‘y‘: None, ‘x‘: None, ‘z‘: None} #快速创建,会生成一个新的字典,这是字典的内置方法之一
2、访问字典的值
>>>dict1 = {‘a‘:1,‘b‘:2,‘c‘:3} >>>dict1[‘b‘]#获取某元素的值 2 >>>for key,value in dict1.iteritems():#遍历字典 print key,value ... a 1 c 3 b 2
3、更新字典
>>>dict1 = {‘a‘:1,‘b‘:2,‘c‘:3} >>>dict1[‘b‘] = ‘b‘#更新已有条目 >>>dict1 {‘c‘:3,‘a‘:1,‘b‘:‘b‘} >>>dict1 = {‘a‘:1,‘b‘:2,‘c‘:3} >>>dict1[‘d‘] = 4#增加新条目 >>>dict1 {‘a‘:1,‘d‘:4,‘b‘:2,‘c‘:3}
4、删除元素和字典
>>>dict1 = {‘a‘:1,‘b‘:2,‘c‘:3} >>>del dict1[‘b‘]#删除名为’b‘的条目 >>>dict1 {‘c‘:3,‘a‘:1} >>>dict1 = {‘a‘:1,‘b‘:2,‘c‘:3} >>>dict1.clear()#删除字典的所有条目 >>>dict1 {} >>>dict1 = {‘a‘:1,‘b‘:2,‘c‘:3} >>>dict1.pop(‘b‘)#删除并返回该键的值 2 >>>dict1 = {‘a‘:1,‘b‘:2,‘c‘:3} >>>del dict1#删除整个字典
内置方法:
>>>dir(dict) [‘__class__‘, ‘__cmp__‘, ‘__contains__‘, ‘__delattr__‘, ‘__delitem__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘,
‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__setattr__‘, ‘__setitem__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘,
‘clear‘, ‘copy‘, ‘fromkeys‘, ‘get‘, ‘has_key‘, ‘items‘, ‘iteritems‘, ‘iterkeys‘, ‘itervalues‘, ‘keys‘, ‘pop‘, ‘popitem‘, ‘setdefault‘, ‘update‘,
‘values‘, ‘viewitems‘, ‘viewkeys‘, ‘viewvalues‘]
>>>dict1 = {‘a‘:1,‘c‘:3,‘d‘:4,‘a‘:1} >>>dict1.clear() {}
copy
>>>dict1 = {‘a‘:1,‘c‘:3,‘d‘:4,‘b‘:2} >>>dict2 = dict1.copy()#浅复制 >>>dict2 {‘a‘:1,‘c‘:3,‘d‘:4,‘b‘:2}
>>>ddict = {}.fromkeys((‘x‘,‘y‘,‘z‘),-1)#指定值 >>>ddict {‘y‘: -1, ‘x‘: -1, ‘z‘: -1} >>>edict = {}.fromkeys((‘x‘,‘y‘,‘z‘))#默认值为None >>>edict {‘y‘: None, ‘x‘: None, ‘z‘: None}
‘‘‘ D.update([E, ]**F) -> None. Update D from dict/iterable E and F. If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k] ‘‘‘ >>>dict1 = {‘b‘:2,‘c‘:3,‘a‘:1,‘d‘:4} >>>dict2 = {‘abc‘:‘123‘,‘d‘:‘delious‘,‘c‘:3} >>>dict1.update(dict2) >>>dict1 {‘a‘: 1, ‘c‘: 3, ‘b‘: 2, ‘d‘: ‘delious‘, ‘abc‘: ‘123‘}
>>>dict1 = {‘a‘:1,‘b‘:2,‘c‘:3} >>>dict1.pop(‘b‘)#删除并返回该键的值 2 >>>dict1 = {‘a‘:1,‘b‘:2,‘c‘:3} >>>dict1.popitems()#随机删除字典中的一对键值,以元组形式返回 (‘c‘:3)
>>>dict1 = {‘b‘:2,‘c‘:3,‘a‘:1,‘d‘:4} >>>dict1.get(‘b‘)#获取该键的值 2 >>>dict.get(‘bbb‘)#键不存在,默认返回None None >>>dict.get(‘bbb‘‘,‘not exist‘)#可以设置当键不存在时的默认返回值 not exist >>>dict1.has_keys(‘d‘)#判断该键存在于字典中,返回True,否则False True >>>dict1.has_keys(‘ddddd‘) False
>>>dict1 = {‘b‘:2,‘c‘:3,‘a‘:1,‘d‘:4} #获得所有键 >>>dict1.keys()#返回列表 [‘a‘, ‘c‘, ‘b‘, ‘d‘] >>>dict1.iterkeys()#返回可迭代对象 <dictionary-keyiterator object at 0x000000000368F778> >>>dict1.viewkeys()#返回集合 dict_keys([‘a‘, ‘c‘, ‘b‘, ‘d‘]) #获得所有的值 >>>dict1.values()#返回列表 [1, 3, 2, 4] >>>dict1.itervalues()#返回可迭代对象 <dictionary-valueiterator object at 0x00000000038DF778> >>>dct1.viewvalues()#返回集合 dict_values([1, 3, 2, 4]) #获得所有的键值对 >>>dict1.items()#返回列表 [(‘a‘, 1), (‘c‘, 3), (‘b‘, 2), (‘d‘, 4)] >>>dict1.iteritems()#返回可迭代对象 <dictionary-itemiterator object at 0x000000000366F778> >>>dict1.viewitems()#返回集合 dict_items([(‘a‘, 1), (‘c‘, 3), (‘b‘, 2), (‘d‘, 4)])
‘‘‘ D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D ‘‘‘ >>>dict1 = {‘b‘:2,‘c‘:3,‘a‘:1,‘d‘:4} >>>dict1.setdefault(‘a‘,5) 1 >>>dict1 {‘b‘:2,‘c‘:3,‘a‘:1,‘d‘:4} >>>dict1.setdefault(‘e‘,5) 5 >>>dict1 {‘b‘:2,‘c‘:3,‘a‘:1,‘e‘:5,‘d‘:4}
标签:返回值 ... false python对象 -- eth class div 容器
原文地址:http://www.cnblogs.com/Wolverine-python/p/7678574.html