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()方法的更多相关文章

  1. python 字典内置方法get应用

    python字典内置方法get应用,如果我们需要获取字典值的话,我们有两种方法,一个是通过dict['key'],另外一个就是dict.get()方法. 今天给大家分享的就是字典的get()方法. 这 ...

  2. 遍历python字典几种方法

    遍历python字典几种方法 from: http://ghostfromheaven.iteye.com/blog/1549441 aDict = {'key1':'value1', 'key2': ...

  3. Python 字典(Dictionary) clear()方法

    Python 字典(Dictionary) clear()方法 描述 Python 字典(Dictionary) clear() 函数用于删除字典内所有元素.高佣联盟 www.cgewang.com ...

  4. Python 字典(Dictionary) type()方法

    Python 字典(Dictionary) type()方法 描述 Python 字典(Dictionary) type() 函数返回输入的变量类型,如果变量是字典就返回字典类型.高佣联盟 www.c ...

  5. Python 字典(Dictionary) str()方法

    Python 字典(Dictionary) str()方法 描述 Python 字典(Dictionary) str() 函数将值转化为适于人阅读的形式,以可打印的字符串表示.高佣联盟 www.cge ...

  6. Python 字典(Dictionary) len()方法

    Python 字典(Dictionary) len()方法 描述 Python 字典(Dictionary) len() 函数计算字典元素个数,即键的总数.高佣联盟 www.cgewang.com 语 ...

  7. Python 字典(Dictionary) cmp()方法

    Python 字典(Dictionary) cmp()方法 描述 Python 字典的 cmp() 函数用于比较两个字典元素.高佣联盟 www.cgewang.com 语法 cmp()方法语法: cm ...

  8. Python字典内置方法

    Python字典包含了以下内置方法: 序号 函数及描述 1 radiansdict.clear()删除字典内所有元素 2 radiansdict.copy()返回一个字典的浅复制 3 radiansd ...

  9. Python 字典(Dictionary) get()方法

    描述 Python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 get()方法语法: dict.get(key, default=None) 参数 ...

  10. Python 字典(Dictionary) setdefault()方法

    描述 Python 字典(Dictionary) setdefault() 函数和get()方法类似, 如果键不已经存在于字典中,将会添加键并将值设为默认值. 语法 setdefault()方法语法: ...

随机推荐

  1. DNN优势

  2. Broadcast Intent & Broadcast Receiver

    当Android系统发生某种状况,必须通知所有程序进行处理时,例如电量不足等,可利用Broadcast Intent对象的功能来进行信息广播. 运行机制包括两部:送出Intent对象的程序:监听广播信 ...

  3. 上传项目至GitHub

      在windows系统客户端安装git工具. 注:如何安装git工具在此不做介绍,如需了解可网上搜索安装介绍.   在“本地文件”中添加“.git文件”,用于git管理. 进入本地文件夹,右击鼠标- ...

  4. Data Structure Linked List: Write a function to get the intersection point of two Linked Lists.

    http://www.geeksforgeeks.org/write-a-function-to-get-the-intersection-point-of-two-linked-lists/ 第一第 ...

  5. 前端基础-CSS属性操作

    前端基础-CSS属性操作 css text 文本颜色:color 颜色属性被用来设置文字的颜色. 颜色是通过CSS最经常的指定: 十六进制值 - 如: #FF0000 一个RGB值 - 如: RGB( ...

  6. 深入理解ES6之迭代器与生成器

    迭代器 迭代器 iterator,在 Javascript 中,迭代器是一个对象(也可称作为迭代器对象),它提供了一个 next() 方法,用来返回迭代序列中的下一项. next 方法的定义,next ...

  7. systemd基本使用

    参考金步国翻译的systemd中文手册: http://www.jinbuguo.com/systemd/index.html 金步国翻译质量都很高, 非常适合做参考 原文:https://wiki. ...

  8. Jboss添加Windows服务,同时定期重启

    一.添加成Windows服务 进入目录 \wildfly-9.0.2s - All\bin\service\ 编辑 service.bat,修改一下参数 set SHORTNAME=SAMEXAppS ...

  9. 算法(Algorithms)第4版 练习 2.2.9

    package com.qiusongde; import edu.princeton.cs.algs4.In; import edu.princeton.cs.algs4.StdOut; publi ...

  10. Kafka- Kafka架构功能

    Kafka是一个高吞吐量的分布式消息系统,一个分布式的发布-订阅消息系统.Kafka是一种快速,可拓展的,设计内在就是分布式的,分区的可复制的提交日志服务. Apache Kafka与传统消息系统相比 ...