python实现从字典中删除元素的方法

yipeiwu_com5年前Python基础

本文实例讲述了python实现从字典中删除元素的方法。分享给大家供大家参考。具体分析如下:

python的字典可以通过del方法进行元素删除,下面的代码详细演示了这一过程

# Create an empty dictionary
d = {}
# Add an item
d["name"] = "Fido"
assert d.has_key("name")
# Delete the item
del d["name"]
assert not d.has_key("name")
# Add a couple of items
d["name"] = "Fido"
d["type"] = "Dog"
assert len(d) == 2
# Remove all items
d.clear()
assert len(d) == 0

希望本文所述对大家的Python程序设计有所帮助。

相关文章

pandas实现将日期转换成timestamp

pandas实现将日期转换成timestamp

OUTLINE 常见的时间字符串与timestamp之间的转换 日期与timestamp之间的转换 常见的时间字符串与timestamp之间的转换 这里说的字符串不是一般意义上的字符串,...

python求最大值最小值方法总结

python求最大值最小值方法总结

方法一(常规): 代码: count = int(input('输入数据个数:\n')) a = 1 while a <= count: num = int(input(...

Python排序搜索基本算法之选择排序实例分析

Python排序搜索基本算法之选择排序实例分析

本文实例讲述了Python排序搜索基本算法之选择排序。分享给大家供大家参考,具体如下: 选择排序就是第n次把序列中最小的元素排在第n的位置上,一旦排好就是该元素的绝对位置。代码如下:...

在Python中使用SimpleParse模块进行解析的教程

与大多数程序员一样,我经常需要标识存在于文本文档中的部件和结构,这些文档包括:日志文件、配置文件、分隔的数据以及格式更自由的(但还是半结构化的)报表格式。所有这些文档都拥有它们自己的“小...

对django views中 request, response的常用操作详解

request 获取post请求中的json数据 def hello(request): data = json.loads(request.body) ... json格式还...