除了定义和切片外,这里总结下系列的操作:

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

  1. 初识python 字符串 列表 字典相关操作

    python基础(一): 运算符: 算术运算: 除了基本的+ - * / 以外,还需要知道 :  // 为取整除 返回的市商的整数部分 例如: 9 // 2  ---> 4  , 9.0 //  ...

  2. python 基础篇 04(列表 元组 常规操作)

    本节主要内容:1. 列表2. 列表的增删改查3. 列表的嵌套4. 元组和元组嵌套5. range 一. 列表1.1 列表的介绍列表是python的基础数据类型之一 ,其他编程语言也有类似的数据类型. ...

  3. Python基础——列表、元组操作

    列表.元组操作 列表: 列表是Python中最基本的数据结构,列表是最常用的Python数据类型,列表的数据项不需要具有相同的类型.列表中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0 ...

  4. python基础之 列表、元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码

    本节内容 列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 1. 列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 定义列表 ...

  5. 011.Python的列表的相关操作

    一 列表的相关操作 1.1  列表的拼接 lst1 = [1,2,3] lst2 = [4,5,6] res = lst1 + lst2 print(res) 执行 [root@node10 pyth ...

  6. 小白的Python之路 day2 列表、元组操作

    1. 列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 定义列表 1 names = ['Tom','Jack','Qian'] 通过下标访问列表中 ...

  7. 【python系统学习06】一张图看懂列表并学会操作

    点击跳转-原文地址 数据类型 - 列表(list) 「目录:」 一张图了解列表 列表是什么 列表长啥样 语法格式 代码示例 格式特征 列表定义 列表操作 - 提取单个:偏移量 什么是偏移量 偏移量提取 ...

  8. Python系列之 - python数据类型

    原链接:https://blog.csdn.net/m0_37745438/article/details/79572884 学习一门语言,往往都是从Hello World开始. 但是笔者认为,在一个 ...

  9. 获取列表的索引操作:enumerate

    通过循环获取列表的索引操作: 主要使用:enumerate product_list = [['Iphone7',5800], ['Coffee',30], ['疙瘩汤',10], ['Python ...

随机推荐

  1. 对yield 的理解

    最近在学习Python的时候看到yield的相关语法,感觉很独特,相比其他如C/C++的语法比较有意思,于是在看完资料相关章节做一个总结. yield 是一个类似于 return的语法,但是对于ret ...

  2. VR全景:“互联网+之后的下一个“风口”

    2017年VR虚拟现实会成为流行趋势吗? 2017年,另一个时代正在悄然走来--720全景时代!如果你错过了前十年的互联网大爆发,千万不要再错过接下来十年的VR全景时代的机遇! VR全景是" ...

  3. cpp(第十三章)

    1.动态(晚期)联编需要显示定义复制构造函数,赋值运算符,虚构函数. 2.纯虚类不能声明对象. 3.赋值运算符的特征标随类而异. 4.返回类型协变,重新定义继承的方法,应确保与原来的原型完全相同,但如 ...

  4. 警告: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.jee.server:JsonBlog' did not find a matching property.

    这个问题困扰很久了,逛了很多论坛,终于得以解决 我的控制台错误如下: 五月 , :: 下午 org.apache.catalina.startup.VersionLoggerListener log ...

  5. jdbc的配置及jdbc连接常用数据库(mysql、sqlserver、Oracle)

    1.连接SQL Server数据库 import java.sql.*; publicclassMain{publicstaticvoid main(String[] args){String dri ...

  6. python+selenium遇到鼠标悬停不成功可以使用js进行操作

    问题:在定位这种悬停后出现下拉操作的时候,尝试了使用move_to_element的方法 # ele_logout = br.find_element_by_xpath('/html/body/div ...

  7. 制作Ubuntu Kylin局域网源

    国人参与开发的开源操作系统UbuntuKylin(http://www.ubuntukylin.com/)已经发布有一段时间了,一直想在单位的局域网内部用用,可惜离线安装比较麻烦,于是搜索了些如何制作 ...

  8. An abandoned sentiment from past

    An abandoned sentiment from past time limit per test 1 second memory limit per test 256 megabytes in ...

  9. Linux: Bash基本命令

    切换目录 cd 查看当前目录 pwd 生成目录 mkdir 搜索文件 查看当前的文件 ls 删除文件但保留特定类型 rm !(**) 例如: rm !(.tex|*.eps)其中,.tex, .eps ...

  10. 用超链接a来提交form表单

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...