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

yipeiwu_com6年前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程序设计有所帮助。

相关文章

Python3 中把txt数据文件读入到矩阵中的方法

1.实例程序: ''' 数据文件:2.txt内容:(以空格分开每个数据) 1 2 2.5 3 4 4 7 8 7 ''' from numpy import * A = zeros...

Python实现简单的可逆加密程序实例

本文实例讲述了Python实现简单的可逆加密程序。分享给大家供大家参考。具体如下: Python代码如下: 复制代码 代码如下:#coding=utf-8   ''...

详解C++编程中一元运算符的重载

可重载的一元运算符如下: !(逻辑“非”) &(取址) ~(二进制反码) *(取消指针引用) +(一元加) -(一元求反) ++(递增) --(递减)...

21行Python代码实现拼写检查器

引入 大家在使用谷歌或者百度搜索时,输入搜索内容时,谷歌总是能提供非常好的拼写检查,比如你输入 speling,谷歌会马上返回 spelling。 下面是用21行python代码实现的一...

python smtplib模块自动收发邮件功能(二)

python smtplib模块自动收发邮件功能(二)

接上篇python smtplib模块自动收发邮件功能(一) ,用python smtplib模块实现了发送邮件程序了,那么接下来我们需要现在要解决的问题如何在 test_report\...