Python 字典 popitem() 方法

Python 教程 · 2019-02-03 09:29:22

描述

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

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

语法

popitem()方法语法:

popitem()

参数

返回值

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

实例

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

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

site= {'name': '码农教程', 'alexa': 10000, 'url': 'www.codercto.com'}
pop_obj=site.popitem()
print(pop_obj)
print(site)

输出结果为:

('url', 'www.codercto.com')
{'alexa': 10000, 'name': '\xe8\x8f\x9c\xe9\xb8\x9f\xe6\x95\x99\xe7\xa8\x8b'}

点击查看所有 Python 教程 文章: https://www.codercto.com/courses/l/8.html

查看所有标签

R Cookbook

R Cookbook

Paul Teetor / O'Reilly Media / 2011-3-22 / USD 39.99

With more than 200 practical recipes, this book helps you perform data analysis with R quickly and efficiently. The R language provides everything you need to do statistical work, but its structure ca......一起来看看 《R Cookbook》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器