python--字典

来源:互联网 发布:ps淘宝美工教程百度云 编辑:程序博客网 时间:2024/05/09 21:34
>>> a = {1:'one',2:'two',3:'three'}
>>> a
{1: 'one', 2: 'two', 3: 'three'}
>>> b = a.copy()
>>> b
{1: 'one', 2: 'two', 3: 'three'}
>>> a
{1: 'one', 2: 'two', 3: 'three'}
>>> id(a)
47139216
>>> id(b)
47182688
>>> c = a
>>> id (c)
47139216
>>> a.clear()
>>> a
{}
>>> b
{1: 'one', 2: 'two', 3: 'three'}
>>> c
{}
>>>
>>>
>>>
>>> a.pop(2)
Traceback (most recent call last):
  File "<pyshell#80>", line 1, in <module>
    a.pop(2)
KeyError: 2
>>> b.pop(2)
'two'
>>> b
{1: 'one', 3: 'three'}
>>> a.popitem()
Traceback (most recent call last):
  File "<pyshell#83>", line 1, in <module>
    a.popitem()
KeyError: 'popitem(): dictionary is empty'
>>> b.popitem()
(3, 'three')
>>> b
{1: 'one'}
0 0