1.列举字典的值、键与对
>>> d={'a':1,'b':2,'c':3,'d':4} >>> list(d.keys ()) ['c', 'd', 'a', 'b'] >>> list(d.values ()) [3, 4, 1, 2] >>> list(d.items ()) [('c', 3), ('d', 4), ('a', 1), ('b', 2)] >>>
2.获取相对位置
>>> d={'a':1,'b':2,'c':3,'d':4} >>> d.get ('a') 1 >>>
3.合并(注意:不能够使用+,只能够使用update)
>>> d={'a':1,'b':2,'c':3,'d':4} >>> e={'e':5} >>> d+e Traceback (most recent call last): File "<pyshell#30>", line 1, in <module> d+e TypeError: unsupported operand type(s) for +: 'dict' and 'dict' >>> d.update (e) >>> d {'c': 3, 'e': 5, 'd': 4, 'a': 1, 'b': 2} >>>
4.删除
>>> d={'a':1,'b':2,'c':3,'d':4} >>> d.pop ('a') 1 >>> d {'c': 3, 'd': 4, 'b': 2} >>> del d['b'] >>> d {'c': 3, 'd': 4} >>>
就说到这里,谢谢大家
------------------------------------------------------------------
版权声明:本文为博主原创文章,未经博主允许不得转载。