python删除字典中元素的方法:

1、使用clear()方法删除字典内所有元素
clear()方法语法:dict.clear()
示例:
dict = {'Name': 'Zara', 'Age': 7};
print "Start Len : %d" %  len(dict)
dict.clear()
print "End Len : %d" %  len(dict)输出结果如下:
Start Len : 2
End Len : 0
2、使用pop()方法删除字典给定键key及对应的值
pop()方法语法:pop(key[,default])
>>> x = {'a':1,'b':2}
>>> x.pop('a')
1
>>> x
{'b': 2}3、使用popitem() 方法返回并删除字典中的最后一对键和值。
popitem()方法语法:popitem()
>>> x
{'url': 'www.iplaypython.com', 'title': 'python web site'}
>>> x.popitem()
('url', 'www.iplaypython.com')
>>> x
{'title': 'python web site'}            
                分享题目:创新互联Python教程:python怎样删除字典中的元素
                
                网页URL:http://www.csdahua.cn/qtweb/news26/428976.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网