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

相关文章

python实现连续变量最优分箱详解--CART算法

关于变量分箱主要分为两大类:有监督型和无监督型 对应的分箱方法: A. 无监督:(1) 等宽 (2) 等频 (3) 聚类 B. 有监督:(1) 卡方分箱法(ChiMerge) (2) I...

Python读写ini文件的方法

本文实例讲述了Python读写ini文件的方法。分享给大家供大家参考。具体如下: 比如有一个文件update.ini,里面有这些内容: [ZIP] EngineVersion=0 D...

Python 支持向量机分类器的实现

支持向量机(Support Vector Machine, SVM)是一类按监督学习(supervised learning)方式对数据进行二元分类的广义线性分类器(generalize...

python实现简单socket通信的方法

本文实例讲述了python实现简单socket通信的方法。分享给大家供大家参考,具体如下: 刚刚开始接触python,实现了一个helloworld程序---关于udp协议的socket...

Python字符串逆序的实现方法【一题多解】

/post/156406.htm /post/156407.htm 1. 使用索引 >> strA = 'abcdefg' >> strA[::-1] 'gf...