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

# 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. 一天搞定HTML----标签类型与类型转换05

    标签类型: 标签只有两类:行内元素和块元素 行内元素:内容撑开宽高 块元素:默认独占一行 注意: 在使用display时,会遇到一种inline-block类型的标签.这种标签不属于标签的分类. 1. ...

  2. 微信小程序多张图片上传

    微信小程序上传图片每次只能上传一张,所有很多朋友就会问想要多张图片上传怎么办? 首先,我们来看一看wx.chooseImage(object)和wx.uploadFile(OBJECT)这两个个api ...

  3. 【easyui】Tab的tools按钮刷新当前tab

    点击刷新按钮,刷新当前Tab选项卡 /** * Name 选项卡初始化 */ $('#home-tabs').tabs({ tools: [{ iconCls: 'icon-reload', bord ...

  4. linux切换g++

    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.4 50(优先级) sudo update-alternative ...

  5. STM8程序在IAR中报错 unable to allocate space for sections

    Error[Lp011]: section placement failed: unable to allocate space for sections/blocks with a total es ...

  6. APUE-文件和目录(七)符号链接

    符号链接 符号链接的用途 符号链接是对一个文件的间接指针,它与前面介绍的硬连接不同,硬连接指向文件的i节点.引入符号链接是为了避开硬连接的一些限制: 硬链接通常要求链接和文件位于同一文件系统中. 只有 ...

  7. eclipse 下,使用正常模式可以运行,DEBUG模式就卡住的解决方案

    最近开发的时候遇到一个问题,就是用运行就可以编译整个项目,但是使用debug模式就卡住了,编译一部分就不动了.开始我以为是项目太大的关系 但是也不至于就DEBUG不行,所以网上差了些资料,原来是DEB ...

  8. 就是要你懂Java中volatile关键字实现原理

    原文地址http://www.cnblogs.com/xrq730/p/7048693.html,转载请注明出处,谢谢 前言 我们知道volatile关键字的作用是保证变量在多线程之间的可见性,它是j ...

  9. 初玩RAC

    之前因为项目的原因以及ReactiveCocoa框架导入到项目老是报错的原因,导致我这边一直没有能好好的将ReactiveCocoa运行起来,最近看了Hank老师的视频,而且项目中我们使用的就是OC, ...

  10. 使用 Socket.IO 开发聊天室

    前言 Socket.IO 是一个用来实现实时双向通信的框架,其本质是基于 WebSocket 技术. 我们首先来聊聊 WebSocket 技术,先设想这么一个场景: · 用户小A,打开了某个网站的充值 ...