欢迎来到代码驿站!

当前位置:首页 >

对Python 字典元素进行删除的方法

时间:2020-07-31 11:01:05|栏目:|点击:

1. Python字典的clear()方法(删除字典内所有元素)

#!/usr/bin/python
# -*- coding: UTF-8 -*-
dict = {'name': '我的博客地址', 'alexa': 10000, 'url': 'http://blog.csdn.net/uuihoo/'}
dict.clear(); # 清空词典所有条目

2. Python字典的pop()方法(删除字典给定键 key 所对应的值,返回值为被删除的值)

#!/usr/bin/python
# -*- coding: UTF-8 -*-
site= {'name': '我的博客地址', 'alexa': 10000, 'url':'http://blog.csdn.net/uuihoo/'}
pop_obj=site.pop('name') # 删除要删除的键值对,如{'name':'我的博客地址'}这个键值对
print pop_obj # 输出 :我的博客地址

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

#!/usr/bin/python
# -*- coding: UTF-8 -*-
site= {'name': '我的博客地址', 'alexa': 10000, 'url':'http://blog.csdn.net/uuihoo/'}
pop_obj=site.popitem() # 随机返回并删除一个键值对
print pop_obj # 输出结果可能是{'url','http://blog.csdn.net/uuihoo/'}

4. del 全局方法(能删单一的元素也能清空字典,清空只需一项操作)

#!/usr/bin/python
# -*- coding: UTF-8 -*-
site= {'name': '我的博客地址', 'alexa': 10000, 'url':'http://blog.csdn.net/uuihoo/'}
del site['name'] # 删除键是'name'的条目 
del site # 清空字典所有条目

上一篇:Pycharm及python安装详细教程(图解)

栏    目:

下一篇:R语言ggplot2边框背景去除的实现

本文标题:对Python 字典元素进行删除的方法

本文地址:http://www.codeinn.net/misctech/2890.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有