Python 字典 Dictionary popitem() 方法

Python 字典 Dictionary popitem() 方法

Python 字典 DictionaryPython 字典 Dictionary

Python 字典 popitem() 方法返回并删除字典中的最后一对键和值。

如果字典已经为空,却调用了此方法,就报出 KeyError 异常。

 

语法

popitem()方法语法:

popitem()

 

参数

 

返回值

返回一个键值对(key,value)形式。

 

实例

以下实例展示了 popitem() 方法的使用方法:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

site= {'name': '<a href="http://www.yapf.com" target="_blank">硕编程</a>', 'alexa': 10000, 'url': 'www.yapf.com'}
pop_obj=site.popitem()
print(pop_obj)
print(site)

输出结果为:

('url', 'www.yapf.com')
{'alexa': 10000, 'name': '<a href="http://www.yapf.com" target="_blank">硕编程</a>'}

Python 字典 DictionaryPython 字典 Dictionary

下一节:Python 日期和时间

Python 教程

相关文章