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

# 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. js 玩一玩

    闲着没事学了学js,做了一个下页面玩玩. 下面是html代码: <!DOCTYPE html><html> <head> <meta charset=&quo ...

  2. Python3网络爬虫

    # 最近在实验楼学习了爬取妹子图,发现在运行的时候不是很流畅,有些图片下 1 # coding: utf-8 # coding: utf-8 import re import threading fr ...

  3. MQ产品比较-ActiveMQ-RocketMQ

    几种MQ产品说明: ZeroMQ :  扩展性好,开发比较灵活,采用C语言实现,实际上他只是一个socket库的重新封装,如果我们做为消息队列使用,需要开发大量的代码 RabbitMQ :结合erla ...

  4. 框架和css基础

    框架:一.框架集:1.<frameset></frameset>不能有<body>标签 属性:1)cols:把网页拆分成几列(左右拆分)eg:<framese ...

  5. python之matplotlib绘图基础

    Python之matplotlib基础 matplotlib是Python优秀的数据可视化第三方库 matplotlib库的效果可参考 http://matplotlib.org/gallery.ht ...

  6. MyEclipse解决SVN同步冲突问题conflict in the working copy obstructs the current operation

    服务端版本控制软件subversion,客户端是eclipse的插件subclipse.当删除一个东西的时候老是提示错误,说冲突 commit -m "" C:/Users/Adm ...

  7. LANMP一键安装包 版本服务任你选 可安装单一服务

    介绍与使用 更多内容请到 乌龟运维 wuguiyunwei.com 请保证在系统原有yum源文件存在的情况下运行此脚本 以下以centos7.3为例: 下面以安装LNMP为例: ? 1 wget ht ...

  8. Java通过jxl解析Excel文件入库,及日期格式处理方式 (附源代码)

    JAVA可以利用jxl简单快速的读取文件的内容,但是由于版本限制,只能读取97-03  xls格式的Excel. 本文是项目中用到的一个实例,先通过上传xls文件(包含日期),再通过jxl进行读取上传 ...

  9. .NET Framework 各个版本介绍

    .NET Framework 1.1 自1.0版本以来的改进:自带了对mobile asp .net控件的支持.这在1.0版本是以附加功能方式实现的,现在已经集成到框架的内部.安全方面的变更 - 使得 ...

  10. 【基础】Asp.Net操作Cookie总结

    一.什么是Cookie? Cookie是存储在客户端文件系统的文本文件或客户端浏览器对话的内存中的少量数据.它主要用来跟踪数据设置,例如:当我们要访问一个网站网页的时候,用户请求网页时,应用程序可能会 ...