python中pop用法_Python dict pop()用法及代码示例
Python语言为几乎所有容器(无论是列表容器还是集合容器)指定了pop()。这篇特别的文章着重说明Python词典提供的pop()方法。这种方法对于经常处理字典的程序员很有用。
用法:dict.pop(key, def)
参数:
key:必须返回并删除其键值对的键。
def:如果指定的键不存在,则返回的默认值。
返回:
如果存在键,则与已删除键/值对关联的值。
如果不存在 key ,则指定为默认值。
KeyError,如果不存在 key 且未指定默认值。
代码1:演示工作pop(),当存在 key 时。
# Python 3 code to demonstrate
# working of pop()
# initializing dictionary
test_dict = { "Nikhil" :7, "Akshat" :1, "Akash" :2 }
# Printing initial dict
print ("The dictionary before deletion:" + str(test_dict))
# using pop to return and remove key-value pair.
pop_ele = test_dict.pop('Akash')
# Printing the value associated to popped key
print ("Value associated to poppped key is:" + str(pop_ele))
# Printing dictionary after deletion
print ("Dictionary after deletion is:" + str(test_dict))
输出:
The dictionary before deletion:{'Nikhil':7, 'Akshat':1, 'Akash':2}
Value associated to poppped key is:2
Dictionary after deletion is:{'Nikhil':7, 'Akshat':1}
的行为pop()当字典中不存在该键时,功能会有所不同。在这种情况下,如果没有提供默认值,它将返回提供的默认值或KeyError。
代码2:演示pop()在没有 key 的情况下的工作
# Python 3 code to demonstrate
# working of pop() without key
# initializing dictionary
test_dict = { "Nikhil" :7, "Akshat" :1, "Akash" :2 }
# Printing initial dict
print ("The dictionary before deletion:" + str(test_dict))
# using pop to return and remove key-value pair
# provided default
pop_ele = test_dict.pop('Manjeet', 4)
# Printing the value associated to popped key
# Prints 4
print ("Value associated to poppped key is:" + str(pop_ele))
# using pop to return and remove key-value pair
# not provided default
pop_ele = test_dict.pop('Manjeet')
# Printing the value associated to popped key
# KeyError
print ("Value associated to poppped key is:" + str(pop_ele))
输出:
The dictionary before deletion:{'Nikhil':7, 'Akshat':1, 'Akash':2}
Value associated to poppped key is:4
Traceback (most recent call last):
File "main.py", line 20, in
pop_ele = test_dict.pop('Manjeet')
KeyError:'Manjeet'
python中pop用法_Python dict pop()用法及代码示例相关推荐
- python中iter函数_Python iter()函数用法详解
Python iter()函数用法实例分析 本文实例讲述了Python iter()函数用法.分享给大家供大家参考,具体如下: python中的迭代器用起来非常灵巧,不仅可以迭代序列,也可以迭代表现出 ...
- python实现关联算法_python实现关联规则算法Apriori代码示例
本篇文章小编给大家分享一下python实现关联规则算法Apriori代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看. 首先导入包含apriori ...
- python中squeeze函数_Python numpy.squeeze()用法及代码示例
当我们要从数组形状中删除一维条目时,将使用numpy.squeeze()函数. 用法: numpy.squeeze(arr, axis=None ) 参数: arr :[数组]输入数组. axis : ...
- python中range 函数_Python range()函数用法图文详解
python内置range()函数的作用是什么?它能返回一系列连续增加的整数,它的工作方式类似于分片,可以生成一个列表对象.range函数大多数时常出现在for循环中,在for循环中可做为索引使用.其 ...
- python中argmin函数_Python numpy.argmin()用法及代码示例
numpy.argmin(array,axis = None,out = None):返回特定轴上数组min元素的索引. 参数: array:Input array to work on axis : ...
- python中最小公倍数函数_Python实现的求解最小公倍数算法示例
这篇文章主要介绍了Python实现的求解最小公倍数算法,涉及Python数值运算.判断等相关操作技巧,需要的朋友可以参考下 本文实例讲述了Python实现的求解最小公倍数算法.分享给大家供大家参考,具 ...
- python中os.path.join()的循环用法_Python中.join()和os.path.join()两个函数的用法详解
Python中有.join()和os.path.join()两个函数,具体作用如下: . join(): 连接字符串数组.将字符串.元组.列表中的元素以指定的字符(分隔符)连接生成一个新的字符串 ...
- python items函数用法,Python中dictionary items()系列函数的用法实例
本文实例讲述了Python中dictionary items()系列函数的用法,对Python程序设计有很好的参考借鉴价值.具体分析如下: 先来看一个示例: import html # availab ...
- Python中yield和yield from的用法
yield 后面接的是 future 对象 调用方 委托生成器 yield from 直接给出循环后的结果 yield from 委托者和子生成器直接通信 yield from 直接处理stopIte ...
最新文章
- 【直播】王茂霖:二手车交易价格预测-千变万化特征工程(河北高校数据挖掘邀请赛)
- WinForm下PictureBox和Panel控件的On_Paint事件有何区别
- “service httpd does not support chkconfig” 問題
- oracle中判断是否为季末,Oracle中取月初,月末,季初,季末及年初,年末时间总结...
- 分享 | 光纤光缆布线基础知识及系统设计
- 线程同步--条件变量
- jbutton 数组创建 java_java-将JButton数组添加到JPanel(按钮不可见)
- 【Java深入理解】String str = “a“ + “b“ + “c“到底创建了几个对象?
- SQL Server 2019中的图形数据库功能–第1部分
- C#重绘TabControl控件的源码(转)
- 文字投影_DLP投影仪指标含义汇总(文字稿)
- Final Cut Pro X for Mac(fcpx视频剪辑)中文版
- 中国城市交通管理体制改革初探
- RSF 分布式服务框架设计:线程模型
- proto-buf模型格式测试一例
- ntopng安装和基本使用教程
- 释放cuda显存及进程
- JS禁止浏览器打开控制台或查看源代码
- 腾讯试水6点下班,行业加班文化哪有那么容易破除?
- oracle 层层汇总,人在江湖漂,哪能不挨刀之ORACLE分区表(上)