谈谈Python中pop与remove的用法
remove() 函数用于移除列表中某个值的第一个匹配项。
remove()方法语法: list.remove(obj)
如果obj不在列表中会引发 ValueError 错误,通常先使用count方法查看有多少个obj
pop() 函数用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值。
pop()方法语法: list.pop(obj=list[-1])
接下来发现网上的另一篇文章貌似说的不是很合理
https://www.jb51.net/article/132501.htm
a_list = ['a', 'b', 'c', 'd', 'e']
b_list = ['b', 'c']
for i in a_list:
if i in b_list:
a_list.remove(i)
print(a_list)
# 输出 ['a', 'c', 'd', 'e'] a_list = ['a', 'b', 'c', 'd', 'e']
b_list = ['b', 'c']
for i in a_list:
if i in b_list:
idl = a_list.index(i)
a_list.pop(idl)
print(a_list)
# 输出 ['a', 'c', 'd', 'e']
为什么元素‘c’未被删除呢?那篇文章说x已经不是原来的x,好吧先看看以下的代码吧
x = ['a', 'b', 'c', 'd']
print(id(x))
x.remove('b')
print(x)
print(id(x))
#
# ['a', 'c', 'd']
# y = ['a', 'b', 'c', 'd']
print(id(y))
y.pop(2)
print(y)
print(id(y))
#
# [1, 2, 4]
#
这很明显经过remove与pop删除元素之后,地址并没有改变,所以应该不是重新赋值。
针对使用for循环删除元素来谈一谈个人看法,为了方便表达,直接解释代码,如下
a_list = ['a', 'b', 'c', 'c', 'd', 'e']
# 在元素‘c’后面又增加一个‘c’ b_list = ['b', 'c']
for i in a_list:
if i in b_list:
a_list.remove(i)
print(a_list)
# 依然输出 ['a', 'c', 'd', 'e'] ,这说明
# 当remove删除‘b’元素时第一个‘c’移动到‘b’的位置
# 第二遍循环遍历时for循环是从上一次循环的下个索引位置开始的
# 此时就删除了第二个‘c’,第一个“逃过一劫” a_list = ['a', 'b', 'c', 'c', 'd', 'e']
# 在元素‘c’后面又增加一个‘c’ b_list = ['b', 'c']
for i in a_list:
if i in b_list:
idl = a_list.index(i)
a_list.pop(idl)
print(a_list)
# 同样输出 ['a', 'c', 'd', 'e']
# 原理同remove相同
谈谈Python中pop与remove的用法的更多相关文章
- python中pop()与split()的用法
imglist = ['11.jpg','12.jpg','13.jpg','14.jpg','2.jpg','1.jpg',] print(str(imglist)) a = str(imglist ...
- 简单说明Python中的装饰器的用法
简单说明Python中的装饰器的用法 这篇文章主要简单说明了Python中的装饰器的用法,装饰器在Python的进阶学习中非常重要,示例代码基于Python2.x,需要的朋友可以参考下 装饰器对与 ...
- Python中【__all__】的用法
Python中[__all__]的用法 转:http://python-china.org/t/725 用 __all__ 暴露接口 Python 可以在模块级别暴露接口: __all__ = [&q ...
- python中enumerate()函数用法
python中enumerate()函数用法 先出一个题目:1.有一 list= [1, 2, 3, 4, 5, 6] 请打印输出:0, 1 1, 2 2, 3 3, 4 4, 5 5, 6 打印输 ...
- Python中try...except...else的用法
Python中try...except...else的用法: try: <语句>except <name>: <语句> #如果在try ...
- Python中logging模块的基本用法
在 PyCon 2018 上,Mario Corchero 介绍了在开发过程中如何更方便轻松地记录日志的流程. 整个演讲的内容包括: 为什么日志记录非常重要 日志记录的流程是怎样的 怎样来进行日志记录 ...
- (转)Python中的split()函数的用法
Python中的split()函数的用法 原文:https://www.cnblogs.com/hjhsysu/p/5700347.html Python中有split()和os.path.split ...
- Python中zip()与zip(*)的用法
目录 Python中zip()与zip(*)的用法 zip() 知识点来自leetcode最长公共前缀 Python中zip()与zip(*)的用法 可以看成是zip()为压缩,zip(*)是解压 z ...
- python中的随机函数random的用法示例
python中的随机函数random的用法示例 一.random模块简介 Python标准库中的random函数,可以生成随机浮点数.整数.字符串,甚至帮助你随机选择列表序列中的一个元素,打乱一组数据 ...
随机推荐
- meshing-三棱锥结构化网格
原视频下载地址: https://yunpan.cn/cqcq2gE6Iy2P8 访问密码 7d5a
- GO 跟C++/C差异
规范的语法(不需要符号表来解析) 垃圾回收(独有) 无头文件 明确的依赖 无循环依赖 常量只能是数字 int和int32是两种类型 字母大小写设置可见性(letter case sets visibi ...
- Java核心复习 —— J.U.C 并发工具类
一.CountDownLatch 文档描述 A synchronization aid that allows one or more threads to wait until* a set of ...
- Python 死循环
while True: try: x=int(input("Please enter a number:")) break except ValueError: print(&qu ...
- Spring Bootz之热部署
在项目的pom.xml文件添加如下两段 <dependency> <groupId>org.springframework.boot</groupId> <a ...
- 画布之ShapeDrawable
package com.loaderman.customviewdemo; import android.content.Context; import android.graphics.Canvas ...
- Springboot整合Elasticsearch报错availableProcessors is already set to [4], rejecting [4]
Springboot整合Elasticsearch报错 今天使用SpringBoot整合Elasticsearch时候,相关的配置完成后,启动项目就报错了. nested exception is j ...
- Dubbo -- 关于 api接口调用不使用强依赖
首先,我们都知道 Dubbo 调用api 需要提供暴露 接口, 消费端才通过 ZK 可以调用 通常我们都会使用 提供 api jar包 的方式 使用 这样既方便又快捷 简单 只需要在spr ...
- mysql导入、导出 ( 带视图)
1创建账号授权 grant all privileges on jenkinsddbes.* to 'jenkinsddbes'@'%' identified by '1iN@Da12tA&* ...
- Django:reverse反转URL并传递参数
需求: 假设在文章详情页评论文章后需要重新刷新显示该页面(原始方法,提交评论表单为form方式,未采用ajax方式), 提交评论后代码会走comment的视图函数,等数据入库之后需要将页面重新定位到文 ...