列表的系列操作(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 ...
随机推荐
- qrcode生成二维码插件
今天我要和大家分享的是利用qrcode来生成二维码. 首先要使用qrcode就需要引用文件,我这边用的是1.7.2版本的jquery加上qrcode <script type="tex ...
- JVM-3.内存
目录 一.运行时数据区 二.内存使用细节:以HotSpot的堆为例 三.实战:OutOfMemoryError异常 四.垃圾收集器(堆+方法区)与内存分配策略 一.运行时数据区 1.程序计 ...
- twemproxy发送流程探索——剖析twemproxy代码正编
本文想要完成对twemproxy发送流程--msg_send的探索,对于twemproxy发送流程的数据结构已经在<twemproxy接收流程探索--剖析twemproxy代码正编>介绍过 ...
- Unity3d—做一个年月日选择器(Scroll Rect拖动效果优化)— 无限滚动 + 锁定元素
最近..... 废话不多说上效果图 用的是UGUI 我先说思路 通过判断元素的位置信息来改变Hierarchy的顺序 实现无限滚动 改变位置的同时也要不断的调整Content的位置防止乱跳 元素锁定就 ...
- WKWebView 官方文档
WKWebView 类 一个WKWebView对象可以显示交互式的web内容.就像一个应用程序的浏览器.你可以使用WKWebView类嵌入Web内容的应用程序.这样做,创造一个WKWebView对象, ...
- .NET 随记
1. goto 常用于 switch语句中2. 字符串相加用 StringBuilder的Append()方法性能好3. str.Trim(',') 清除字符串后的","4. st ...
- ubuntu eclipse 建立server 提示coud not load the tomcat server configuration at /opt/apache ...的解决方法
ubuntu eclipse 建立server 提示coud not load the tomcat server configuration at /opt/apache ...的解决方法 & ...
- 聊聊RocksDB Compact
| 导语 对于 LevelCompact 策略,RocksDB会根据每一层不同的策略计算出CompactScore,根据CompactScore大小来决定那一层将会优先进行Compact,然后选择Le ...
- Python 内置函数汇总
循环设计与循环对象 range() enumerate() zip() iter() 函数对象 map() filter() reduce() 序列操作 all([True, 1, "hel ...
- R语言重要数据集分析研究——需要整理分析阐明理念
1.R语言重要数据集分析研究需要整理分析阐明理念? 上一节讲了R语言作图,本节来讲讲当你拿到一个数据集的时候如何下手分析,数据分析的第一步,探索性数据分析. 统计量,即统计学里面关注的数据集的几个指标 ...