久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

對Python 字典元素進行刪除的方法

瀏覽:2日期:2022-07-15 16:48:45

1. Python字典的clear()方法(刪除字典內所有元素)

#!/usr/bin/python# -*- coding: UTF-8 -*-dict = {’name’: ’我的博客地址’, ’alexa’: 10000, ’url’: ’http://blog.csdn.net/uuihoo/’}dict.clear(); # 清空詞典所有條目

2. Python字典的pop()方法(刪除字典給定鍵 key 所對應的值,返回值為被刪除的值)

#!/usr/bin/python# -*- coding: UTF-8 -*-site= {’name’: ’我的博客地址’, ’alexa’: 10000, ’url’:’http://blog.csdn.net/uuihoo/’}pop_obj=site.pop(’name’) # 刪除要刪除的鍵值對,如{’name’:’我的博客地址’}這個鍵值對print pop_obj # 輸出 :我的博客地址

3. Python字典的popitem()方法(隨機返回并刪除字典中的一對鍵和值)

#!/usr/bin/python# -*- coding: UTF-8 -*-site= {’name’: ’我的博客地址’, ’alexa’: 10000, ’url’:’http://blog.csdn.net/uuihoo/’}pop_obj=site.popitem() # 隨機返回并刪除一個鍵值對print pop_obj # 輸出結果可能是{’url’,’http://blog.csdn.net/uuihoo/’}

4. del 全局方法(能刪單一的元素也能清空字典,清空只需一項操作)

#!/usr/bin/python# -*- coding: UTF-8 -*-site= {’name’: ’我的博客地址’, ’alexa’: 10000, ’url’:’http://blog.csdn.net/uuihoo/’}del site[’name’] # 刪除鍵是’name’的條目 del site # 清空字典所有條目

以上就是對Python 字典元素進行刪除的方法的詳細內容,更多關于Python 刪除字典元素的資料請關注好吧啦網其它相關文章!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 东平县| 长丰县| 陆河县| 洮南市| 进贤县| 巴林右旗| 治多县| 夏津县| 广饶县| 东阿县| 闻喜县| 张家界市| 连州市| 贡觉县| 汉中市| 本溪市| 三原县| 海城市| 安远县| 林甸县| 西畴县| 桂林市| 泾阳县| 大洼县| 高阳县| 巨鹿县| 武宁县| 吴川市| 米脂县| 保亭| 兴业县| 绥江县| 封开县| 自治县| 汽车| 宁陕县| 蓝山县| 鄢陵县| 宣城市| 广宗县| 外汇|