列表的系列操作(python)
除了定义和切片外,这里总结下系列的操作:
# hanbb come on!
names = ["hbb",'tian','bao','cheng'] #Add
names.append("new")
print(names) #['hbb', 'tian', 'bao', 'cheng', 'new'] # insert
#names.insert(1,before 2) #invalid syntax
names.insert(1,"before 1")
print(names) #['hbb', 'before 1','tian', 'bao', 'cheng', 'new']
names.insert(2,'behind 1') #写在哪,插在哪
print(names) # ['hbb', 'behind 1', 'before 2', 'tian', 'bao', 'cheng', 'new'] # revise
names [0] = "忍你很久了"
print(names) # ['忍你很久了', 'before 1', 'behind 1', 'tian', 'bao', 'cheng', 'new'] # delete
del names [1]
print(names) # ['忍你很久了', 'behind 1', 'tian', 'bao', 'cheng', 'new']
# del names['bao'] #list indices must be integers or slices, not str # names.del # no have this operation
# names.remove(2) # list.remove(x): x not in list
names.remove("tian")
print(names) # ['忍你很久了', 'behind 1', 'bao', 'cheng', 'new'] names.pop(2) # ['忍你很久了', 'behind 1', 'cheng', 'new']
print(names)
names.pop() # delete the last one
print(names) # ['忍你很久了', 'behind 1', 'cheng']
# extend
names_2 = ["cao","hu","zhuo"] names.extend(names_2)
print(names) # ['忍你很久了', 'behind 1', 'cheng', 'cao', 'hu', 'zhuo'] # copy
names_3 = names.copy()
print(names_3) # ['忍你很久了', 'behind 1', 'cheng', 'cao', 'hu', 'zhuo'] # count
# names.count() # count() takes exactly one argument (0 given) #print(names.count(cao)) # name 'cao' is not defined
print(names.count("cao")) # 统计出现的次数 # sort
names.insert(-1,"")
names.insert(-1,"b88")
print(names) # ['忍你很久了', 'behind 1', 'cheng', 'cao', 'hu', '666', 'b88', 'zhuo']
names.sort()
print(names) # ['666', 'b88', 'behind 1', 'cao', 'cheng', 'hu', 'zhuo', '忍你很久了']
# Reverse
names.reverse()
print(names) # ['忍你很久了', 'zhuo', 'hu', 'cheng', 'cao', 'behind 1', 'b88', '666'] # 获取下标(位置)
# names.index() # return first index of value. Raises ValueError if the value is not present.
print(names.index("hu")) # names.insert(2,"hu")
print(names) # ['忍你很久了', 'zhuo', 'hu', 'hu', 'cheng', 'cao', 'behind 1', 'b88', '666']
print(names.index("hu")) #
可以分为两种情况进行总结:
总结1:单元元素操作
names = ["hbb",'tian','bao','cheng']
# the operation of single element:
names.append("xiaoqi") # add to the last
names.insert(2,"bb8") # inset one element to target location. names.remove("bao") # remove one element
names.pop() # remove the last element
names.pop(1) # remove element according to it location
print(names.count("cheng")) # 统计某个元素出现的次数
print(names.index("cheng")) # 获取某个元素的位置(下标),第一次出现 names [2] = "我要上位" # 将某位置的元素换掉
names ["hbb"] = "HBB" # 只能根据位置来操作,很忧伤
del names [1] # 根据位置删除
总结1:整个列表的操作
# the operation of entired list
names_2 = names.copy() # 复制列表
names_3 = ['','']
names.extend(names_3) # 扩展列表 names.reverse() # 翻转列表
names.sort() # 排序
总结2:多数情况均是以names.XXXX()进行操作,有几个不是:
names = ["hbb",'tian','bao','cheng']
# the operation of single element: names [2] = "我要上位" # 更换操作
del names [1] # 可以用 names.remove( )代替
names.pip (1)
# the operation of entired list
names_3 = ['',''] # difine
列表的系列操作(python)的更多相关文章
- 初识python 字符串 列表 字典相关操作
python基础(一): 运算符: 算术运算: 除了基本的+ - * / 以外,还需要知道 : // 为取整除 返回的市商的整数部分 例如: 9 // 2 ---> 4 , 9.0 // ...
- python 基础篇 04(列表 元组 常规操作)
本节主要内容:1. 列表2. 列表的增删改查3. 列表的嵌套4. 元组和元组嵌套5. range 一. 列表1.1 列表的介绍列表是python的基础数据类型之一 ,其他编程语言也有类似的数据类型. ...
- Python基础——列表、元组操作
列表.元组操作 列表: 列表是Python中最基本的数据结构,列表是最常用的Python数据类型,列表的数据项不需要具有相同的类型.列表中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0 ...
- python基础之 列表、元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码
本节内容 列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 1. 列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 定义列表 ...
- 011.Python的列表的相关操作
一 列表的相关操作 1.1 列表的拼接 lst1 = [1,2,3] lst2 = [4,5,6] res = lst1 + lst2 print(res) 执行 [root@node10 pyth ...
- 小白的Python之路 day2 列表、元组操作
1. 列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 定义列表 1 names = ['Tom','Jack','Qian'] 通过下标访问列表中 ...
- 【python系统学习06】一张图看懂列表并学会操作
点击跳转-原文地址 数据类型 - 列表(list) 「目录:」 一张图了解列表 列表是什么 列表长啥样 语法格式 代码示例 格式特征 列表定义 列表操作 - 提取单个:偏移量 什么是偏移量 偏移量提取 ...
- Python系列之 - python数据类型
原链接:https://blog.csdn.net/m0_37745438/article/details/79572884 学习一门语言,往往都是从Hello World开始. 但是笔者认为,在一个 ...
- 获取列表的索引操作:enumerate
通过循环获取列表的索引操作: 主要使用:enumerate product_list = [['Iphone7',5800], ['Coffee',30], ['疙瘩汤',10], ['Python ...
随机推荐
- (原创)用Java实现链表结构对象:单向无环链表
转载请注明本文出处:http://www.cnblogs.com/Starshot/p/6918569.html 链表的结构是由一个一个节点组成的,所谓链,就是每个节点的头尾连在一起.而单向链表就是: ...
- 导入java项目时出现红色叹号问题的解决
问题:导入java项目时出现红色叹号(如下图所示) 原因:引入项目的某些jar包跟自己电脑上的位置不一样: 解决方案:步骤如下 (1)右键红色叹号所在项目————>build path————& ...
- weather API 天气api接口 收集整理
腾讯 http://sou.qq.com/online/get_weather.php?callback=Weather&city=南京 中国天气-weather.com.cn http:// ...
- 关于QT5使用QtScript解析QJsonArray数组的问题
首先得在pro文件中加入QT+=script 然后导入相应的头文件 include <QStringList> #include <QtScript/QScriptEngine> ...
- Vulkan Tutorial 17 Rendering and presentation
操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Visual Studio 2017 Setup 这一章节会把之前的所有内容进行整合.我们将会编写drawFrame函数, ...
- 在项目中利用TX Text Control进行WORD文档的编辑显示处理
在很多文档管理的功能模块里面,我们往往需要对WORD稳定进行展示.编辑等处理,而如果使用微软word控件进行处理,需要安装WORD组件,而且接口使用也不见得简单易用,因此如果有第三方且不用安装Offi ...
- python web -- django
一. 安装 django $ pip install django (env)$ python >> import django >> django.VERSION >& ...
- 并发编程(一):从头到脚解读synchronized
一.目录 1.多线程启动方式 2.synchronized的基本用法 3.深度解析synchronized 4.同步方法与非同步方法是否能同时调用? 5.同步锁是否可重入(可重入锁)? 6.异常是否会 ...
- MFC简单绘制安卓机器人
原始日期:2014-03-29 20:35 众所周知,google的安卓机器人形象十分经典,包括眼睛的位置,胳膊以及天线的位置都是有固定位置和比例的,而且是最恰当的,看起来最美.而微软基础类库MFC绘 ...
- Reids命令解析-RENAME
有一天开发突然照过来问,维萨我这个Redis实例这么慢呢?为什么这么慢,于是连上实例SLOWLOG 一看,这些慢日志都是大部分是RENMAE操作导致的,可是为什么RENAME操作会慢呢?不就是改个名字 ...