list补充,append()、extend()、insert()、remove()、del()、pop()、分片
1.添加
append(object),是指在列表的末尾添加一个元素。
- >>> arr = [1,2,'a','你好',[11,22,33]]
- >>> arr
- [1, 2, 'a', '你好', [11, 22, 33]]
- >>> print(arr[0])
- >>> print(arr[4][2])
- 33
- >>>
- >>> arr.append("小虾米")
extend(list),可以在列表的末尾追加一个列表。
- >>> arr
- [1, 2, 'a', '你好', [11, 22, 33], '小虾米']
- >>> arr.extend(['c','b','a'])
- >>> arr
- [1, 2, 'a', '你好', [11, 22, 33], '小虾米', 'c', 'b', 'a']
insert(index,object),可以在指定的未知插入相应的元素
- >>> arr
- [1, 2, 'a', '你好', [11, 22, 33], '肖泽敏', 'c', 'b', 'a']
- >>>
- >>> arr.insert(0,'我是开头')
- >>> arr
- ['我是开头', 1, 2, 'a', '你好', [11, 22, 33], '肖泽敏', 'c', 'b', 'a']
- >>>
2.删除
remove(element),用作于移除列表中已知的某个元素。
- >>> arr
- ['我是开头', 1, 2, 'a', '你好', [11, 22, 33], '肖泽敏', 'c', 'b', 'a']
- >>> arr.remove(1)
- >>> arr
- ['我是开头', 2, 'a', '你好', [11, 22, 33], '肖泽敏', 'c', 'b', 'a']
- >>>
使用remove()删除指定元素时必须注意如果列表中没有该元素,则程序会报错
- >>> arr
- ['我是开头', 1, 2, 'a', '你好', [11, 22, 33], '肖泽敏', 'c', 'b', 'a']
- >>> arr.remove("")
- Traceback (most recent call last):
- File "<pyshell#18>", line 1, in <module>
- arr.remove("")
- ValueError: list.remove(x): x not in list
- >>>
del(),使用del 删除可以删除整个列表,也可以删除制定位置的元素。
del arr[index]这个是删除制定元素的例子:
- >>> arr
- ['我是开头', 2, 'a', '你好', [11, 22, 33], '肖泽敏', 'c', 'b', 'a']
- >>> del arr[0]
- >>> arr
- [2, 'a', '你好', [11, 22, 33], '肖泽敏', 'c', 'b', 'a']
- >>>
del arr 是删除这个列表,包括这个对像,所以我们在实际开发中需要慎重考虑
- >>> del arr
- >>> arr
- Traceback (most recent call last):
- File "<pyshell#44>", line 1, in <module>
- arr
- NameError: name 'arr' is not defined
- >>>
分片:分片的意思就是取出列表的某一片的数据,这个时候就会用到:
假设我们想去出列表中1到3这一片的元素
- >>> arr[1:3]
- [[11, 22, 33], '肖泽敏']
- >>> arr
- [2, [11, 22, 33], '肖泽敏', 'c', 'b', 'a']
- >>>
分片紧紧是取出这“片”的数据,并不会改变列表本身。
pop(index),用作于删除制定下标的元素,并且返回该元素。也就是说当我们想要删除这个指定下标的元素,但是呢我们可能还会在别的地方用到该元素,那么您就可以采用该删除方法。
- >>> arr
- [2, 'a', '你好', [11, 22, 33], '肖泽敏', 'c', 'b', 'a']
- >>> arr.pop(1)
- 'a'
- >>> arr
- [2, '你好', [11, 22, 33], '肖泽敏', 'c', 'b', 'a']
- >>> temp = arr.pop(1)
- >>> arr
- [2, [11, 22, 33], '肖泽敏', 'c', 'b', 'a']
- >>> temp
- '你好'
- >>>
list补充,append()、extend()、insert()、remove()、del()、pop()、分片的更多相关文章
- append()/extend()/insert()/remove()/del/pop()/slice列表分片
member = ['小甲鱼', 88, '黑夜', 90, '迷途', 85, '怡静', 90, '秋舞斜阳', 88] member.append('字符串')#在列表结尾处增加字符串 memb ...
- 列表:list[1],切片list[1:3],追加insert,修改,删除remove,del,pop,查找index,统计count,清空list.clear() 翻转list.reverse(),排序list.sort(),扩展list.extend,
列表的定义: 列表的使用以及取值:用逗号的方式,取列表两个值,会打印出2个项目,两个项目之间自动有一个空格. 如果想取中间几个值: 请注意,如果取值1和2,那么要写[1,3],要记住这里是顾头不顾尾. ...
- python列表删除--remove(),del,pop()
remove()参数为列表元素,若在列表中,删除,不在则报错 如: pop()不带参数时默认删除列表的末尾元素并返回该元素,带参数时该参数为列表元素的下标值 不带参数: 以下标为参数: del 后面可 ...
- python列表插入--append(), extend(), insert()
append(),extend(), insert()都是列表操作中常用的插入函数.其中前两个均接收一个参数,并插入到列表尾部.最后一个接收两个参数,将参数2插入到参数1之前. 本文主要讨论appen ...
- python remove跟pop的区别
remove根据值来删除 li=[1,2,3,4] li.pop[0] li.remove['] 或者是a=li[-1]li.remove(a) pop是根据索引来删除
- 【Python算法】列表中的 append 比 insert 效率高的实质
append 与 insert 对比: # append 操作 >>> count = 10**5 >>> nums = [] >>> for i ...
- 58-python基础-python3-集合-集合常用方法-删除元素-remove()-discard()-pop()-clear()
删除元素-remove()-discard()-pop()-clear() 1-remove() remove()用于删除一个set中的元素,这个值在set中必须存在,如果不存在的话,会引发KeyEr ...
- python pandas ---Series,DataFrame 创建方法,操作运算操作(赋值,sort,get,del,pop,insert,+,-,*,/)
pandas 是基于 Numpy 构建的含有更高级数据结构和工具的数据分析包 pandas 也是围绕着 Series 和 DataFrame 两个核心数据结构展开的, 导入如下: from panda ...
- 006 列表的三种删除方法 remove,pop,del
先定义一个列表: number=[,'changhao','常浩',5.2] . remove(): number.remove('changhao')---括号内是要删除的单一值 . pop(): ...
- 补充——a.extend(b) #合并列表——a.update(b) #把b字典的元素加入到a字典里面
1.list dict tulpe a='dongtian'a[0]='h' #会报错,字符串不能修改print(a) a=[]a.append('abc')a.insert(0,'vcc') b=[ ...
随机推荐
- 简单贪心) Repair the Wall hdu2124
Repair the Wall http://acm.hdu.edu.cn/showproblem.php?pid=2124 Time Limit: 5000/1000 MS (Java/Others ...
- nginx+keepalived高可用web负载均衡
一:安装环境 准备2台虚拟机,都安装好环境 centos 7keepalived:vip: 192.168.1.112192.168.1.110 nginxip 192.168.1.109 maste ...
- 下载神器(vip下载速度)
简单介绍: 用过好几款下载神器,现在推荐一款比较好用的软件,强调一点本软件强调开源免费的原则,禁止一切人员在其中收取费用. 我把这款软件放到了,自己的百度云盘. 神器的使用教程如下: 百度云下载连接: ...
- Storm 使用手册
一.Storm相关术语: Nimbus: Storm集群主节点,负责资源的分配和任务的调度 Supervisor:Storm集群工作节点,接受Nimbus分配的任务,管理Worker Worker:S ...
- go goroutine
进程和线程 进程是程序在操作系统中的一次执行过程,系统进行资源分配和调度的 一个独立单位. 线程是进程的一个执行实体,是CPU调度和分派的基本单位,它是比进程更 小的能独立运行的基本单位. 一个进程可 ...
- JSON的简单使用_解析前台传来的JSON数据
package cn.rocker.json; import org.junit.Test; import net.sf.json.JSONArray; import net.sf.json.JSON ...
- nginx之正向代理
1.概述 nginx的正向代理,只能代理http.tcp等,不能代理https请求.有很多人不是很理解具体什么是nginx的正向代理.什么是反向代理.下面结合自己的使用做的一个简介: 1)正向代理: ...
- Oracle DataBase 编码格式
sqlplus 查询 Oracle 数据库结果乱码或显示 ? 则需要设置字符集 一.客户端字符集 格式:NLS_LANG=language_territory.charset Language: 指定 ...
- Mastering Markdown
What is markdown? Markdown is a lightweight and easy-to-use syntax for styling all forms writing on ...
- Golang入门教程(十二)安装注意事项
1.$GOPATH (1)go 命令依赖一个重要的环境变量:$GOPATH .注:这个不是Go安装目录 (2) (3) (4)git 安装 (5)包管理对应关系 (6)安装完之后bee 工具后,bee ...