Python:字典的pop()方法
pop():移除序列中的一个元素(默认最后一个元素),并且返回该元素的值。
一)移除list的元素,若元素序号超出list,报错:pop index out of range(超出范围的流行指数);
A、默认移除最后一个元素
list_1 = [1, 2, 3, 4, 5]
a = list_1.pop()
print (list_1, a)
-->[1, 2, 3, 4] 5
B、移除list中的某一个元素:pop(元素序号)
list_1 = [1, 2, 3, 4, 5]
a = list_1.pop(2)
print (list_1, a)
-->[1, 2, 4, 5] 3
二)移除dict中的元素(只能移除1个元素):pop(),()内不能为空,要有key;若key不在dict内,需要主动给出需要返回的数据,否则报错;
A、key在dict_1内
dict_1 = {1:'a', 2:'b', 3:'c', 4:'d', 5:'e'}
a = dict_1.pop(2)
print(dict_1, a)
-->{1:'a', 3:'c', 4:'d', 5:'e'} b
B、key不在dict_1内:返回pop给定的数据
dict_1 = {1:'a', 2:'b', 3:'c', 4:'d', 5:'e'}
a = dict_1.pop(6, [1, 2, 3, 4])
print(dict_1, a)
-->{1:'a', 2:'b', 3:'c', 4:'d', 5:'e'} [1, 2, 3, 4]
Python:字典的pop()方法的更多相关文章
- python 字典内置方法get应用
python字典内置方法get应用,如果我们需要获取字典值的话,我们有两种方法,一个是通过dict['key'],另外一个就是dict.get()方法. 今天给大家分享的就是字典的get()方法. 这 ...
- 遍历python字典几种方法
遍历python字典几种方法 from: http://ghostfromheaven.iteye.com/blog/1549441 aDict = {'key1':'value1', 'key2': ...
- Python 字典(Dictionary) clear()方法
Python 字典(Dictionary) clear()方法 描述 Python 字典(Dictionary) clear() 函数用于删除字典内所有元素.高佣联盟 www.cgewang.com ...
- Python 字典(Dictionary) type()方法
Python 字典(Dictionary) type()方法 描述 Python 字典(Dictionary) type() 函数返回输入的变量类型,如果变量是字典就返回字典类型.高佣联盟 www.c ...
- Python 字典(Dictionary) str()方法
Python 字典(Dictionary) str()方法 描述 Python 字典(Dictionary) str() 函数将值转化为适于人阅读的形式,以可打印的字符串表示.高佣联盟 www.cge ...
- Python 字典(Dictionary) len()方法
Python 字典(Dictionary) len()方法 描述 Python 字典(Dictionary) len() 函数计算字典元素个数,即键的总数.高佣联盟 www.cgewang.com 语 ...
- Python 字典(Dictionary) cmp()方法
Python 字典(Dictionary) cmp()方法 描述 Python 字典的 cmp() 函数用于比较两个字典元素.高佣联盟 www.cgewang.com 语法 cmp()方法语法: cm ...
- Python字典内置方法
Python字典包含了以下内置方法: 序号 函数及描述 1 radiansdict.clear()删除字典内所有元素 2 radiansdict.copy()返回一个字典的浅复制 3 radiansd ...
- Python 字典(Dictionary) get()方法
描述 Python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 get()方法语法: dict.get(key, default=None) 参数 ...
- Python 字典(Dictionary) setdefault()方法
描述 Python 字典(Dictionary) setdefault() 函数和get()方法类似, 如果键不已经存在于字典中,将会添加键并将值设为默认值. 语法 setdefault()方法语法: ...
随机推荐
- 获取exe文件窗口抓图,将memo转化为JPG输出
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...
- Jquery禁用所有checkbox
$("input[type=checkbox]").each(function(){ $(this).attr("disabled",false);});
- SpringBoot_集成Shiro后获取当前用户
//SecurityUtils.getSubject().getPrincipal(); 就可以获取了 protected User getCurrentUser(){ return (User) ...
- Broadcast Intent & Broadcast Receiver
当Android系统发生某种状况,必须通知所有程序进行处理时,例如电量不足等,可利用Broadcast Intent对象的功能来进行信息广播. 运行机制包括两部:送出Intent对象的程序:监听广播信 ...
- R语言做正态性检验
摘自:吴喜之:<非参数统计>(第二版),中国统计出版社,2006年10月:P164-165 1.ks.test() 例如零假设为N(15,0.2),则ks.test(x," ...
- Integer 与 int 中的 ==
public class IntegerTest { public static void main(String args[]){ /** * int == 比较大小 */ int p1 = 100 ...
- 【四】MongoDB索引管理
一.索引介绍 在mongodb中,索引用来支持高效查询.如果没有索引,mongodb必须在整个集合中扫描每个文档来查找匹配的文档.但是如果建立合适的索引,mongodb就可以通过索引来限制检查的文档数 ...
- awk过滤磁盘使用率
过滤出大于某个值的磁盘使用率: #!/bin/bash diskspace=`df -Ph`IFS="\n" for line in $diskspace;do ec ...
- POJ1182 食物链 并查集
#include<iostream>#include<stdio.h>#include<string.h>using namespace std;const int ...
- Linux - xshell上传文件报错乱码
xshell上传文件报错乱码,解决方法 rz -be 回车 下载sz filename