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

相关文章

matplotlib给子图添加图例的方法

matplotlib给子图添加图例的方法

代码如下: import matplotlib.pyplot as plt x = [1,2,3,4,5,6,7,8] y = [5,2,4,2,1,4,5,2] axe1 = p...

在OpenCV里使用Camshift算法的实现

在OpenCV里使用Camshift算法的实现

前面学习过Meanshift算法,在观察这个结果标记时,会发现有这样一个问题,如下图: 汽车比较远时,用一个很小的窗口就可以把它框住,这是符合近大远小的投影原理,当比较近的时候如下:...

解决tensorflow由于未初始化变量而导致的错误问题

我写的这个程序 import tensorflow as tf sess=tf.InteractiveSession() x=tf.Variable([1.0,2.0]) a=tf...

Python利用字典破解WIFI密码的方法

最近看到网上的一些作品,然后进行一些完善。只是用于学习,不要去干坏事哦。程序来源于网,我只是做了一些优化。当然这种方法破解还是有点慢哦。我用的python 3.6.5 既然要破解wifi...

Python实现求一个集合所有子集的示例

方法一:回归实现 def PowerSetsRecursive(items): """Use recursive call to return all subsets of it...