1. Python基础教程
  2. 在SublimeEditor中配置Python环境
  3. Python代码中添加注释
  4. Python中的变量的使用
  5. Python中的数据类型
  6. Python中的关键字
  7. Python字符串操作
  8. Python中的list操作
  9. Python中的Tuple操作
  10. Pythonmax()和min()–在列表或数组中查找最大值和最小值
  11. Python找到最大的N个(前N个)或最小的N个项目
  12. Python读写CSV文件
  13. Python中使用httplib2–HTTPGET和POST示例
  14. Python将tuple开箱为变量或参数
  15. Python开箱Tuple–太多值无法解压
  16. Pythonmultidict示例–将单个键映射到字典中的多个值
  17. PythonOrderedDict–有序字典
  18. Python字典交集–比较两个字典
  19. Python优先级队列示例

Python中,列表为:

  • 有序
  • 索引(索引从0开始)
  • 易变的
  • 异构的(列表中的项目不必是同一类型)
  • 写为方括号之间的逗号分隔值列表
listOfSubjects = ['physics', 'chemistry', "mathematics"]
listOfIds = [0, 1, 2, 3, 4]
miscList = [0, 'one', 2, 'three']

1. Access list items

要访问列表中的值,请使用切片语法或数组索引形式的方括号来获取单个项目或项目范围。

传递的索引值可以是正数或负数。如果索引是负数则从列表的末尾开始计数。

list [m : n]表示子列表从索引m(包括)开始,到索引n(不包括)结束。

  • 如果m未提供,则假定其值为零。
  • 如果n未提供,则选择范围直到列表的最后。
ids = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print( ids[0] ) # 0
print( ids[1:5] ) # [1, 2, 3, 4]
print( ids[ : 3] ) # [0, 1, 2]
print( ids[7 : ] ) # [7, 8, 9]\
print( ids[-8:-5] ) # [2, 3, 4]

2. Modily list

要更改列表中的特定项目,请使用其索引引用该项目并分配一个新值。

charList =  ["a", "b", "c"]
charList [2] = "d"
print (charList) # ['a', 'b', 'd']

3. Iterate a list

我们可以使用来遍历列表项for loop

charList =  ["a", "b", "c"]

for x in charList:
print(x) # a
# b
# c

4. Check if a item exists in the list

使用'in'关键字确定列表中是否存在指定的项目。

charList =  ["a", "b", "c"]
if "a" in charList:
print("a is present") # a is present if "d" in charList:
print("d is present")
else:
print("d is NOT present") # d is NOT present

5. Finding length of the list

使用该len()函数查找给定列表的长度。

charList =  ["a", "b", "c"]
x = len (charList)
print (x) # 3

6. Adding items

  • 要将项目添加到列表的末尾,请使用append(item)方法。
  • 要在特定索引位置添加项目,请使用insert(index, item)方法。如果index大于索引长度,则将项目添加到列表的末尾。
charList =  ["a", "b", "c"]
charList.append("d")
charList.append("e")
print (charList) # ['a', 'b', 'c', 'd', 'e']
charList.insert(5, "f") print (charList) # ['a', 'b', 'c', 'd', 'e', 'f'] charList.insert(10, "h") # No error print (charList) # ['a', 'b', 'c', 'd', 'e', 'f', 'h']

7. Removing items

若要从列表中删除项目,四个途径使用一个,即remove()pop()clear()del关键字。

7.1. remove()

它通过其值删除指定的项目。

charList =  ["a", "b", "c"]
charList.remove("c")
print (charList) # ['a', 'b']

7.2. pop()

它通过索引删除指定的项目。如果未提供index,它将从列表中删除最后一项。

charList =  ["a", "b", "c", "d"]
charList.pop() # removes 'd' - last item
print (charList) # ['a', 'b', 'c']
charList.pop(1) # removes 'b'
print (charList) # ['a', 'c']

7.3. clear()

它清空列表。

charList =  ["a", "b", "c", "d"]
charList.clear()
print (charList) # []

7.4. del keyword

它可以用来从列表的索引中删除项目。我们也可以使用它删除整个列表

charList =  ["a", "b", "c", "d"]
del charList[0] print (charList) # ['b', 'c', 'd'] del charList print (charList) # NameError: name 'charList' is not defined

8. Join two lists

我们可以使用"+"运算符或extend()函数将两个给定的列表加入Python 。

charList = ["a", "b", "c"]
numList = [1, 2, 3] list1 = charList + numList print (list1) # ['a', 'b', 'c', 1, 2, 3] charList.extend(numList) print (charList) # ['a', 'b', 'c', 1, 2, 3]

9. Python list methods

9.1. append()

在列表的末尾添加一个元素。

charList =  ["a", "b", "c"]

charList.append("d")

print (charList)	# ["a", "b", "c", "d"]

9.2. clear()

从列表中删除所有元素。

charList =  ["a", "b", "c"]

charList.clear()

print (charList)	# []

9.3. copy()

返回列表的副本。

charList =  ["a", "b", "c"]
newList = charList.copy()
print (newList) # ["a", "b", "c"]

9.4. count()

返回具有指定值的元素数。

charList =  ["a", "b", "c"]
x = charList.count('a')
print (x) # 1

9.5. extend()

将列表的元素添加到当前列表的末尾。

charList = ["a", "b", "c"]
numList = [1, 2, 3]
charList.extend(numList) print (charList) # ['a', 'b', 'c', 1, 2, 3]

9.6. index()

返回具有指定值的第一个元素的索引。

charList =  ["a", "b", "c"]
x = charList.index('a')
print (x) # 0

9.7. insert()

在指定位置添加元素。

charList =  ["a", "b", "c"]
charList.insert(3, 'd')
print (charList) # ['a', 'b', 'c', 'd']

9.8. pop()

删除指定位置或列表末尾的元素。

charList =  ["a", "b", "c", "d"]
charList.pop() # removes 'd' - last item
print (charList) # ['a', 'b', 'c']
charList.pop(1) # removes 'b'
print (charList) # ['a', 'c']

9.9. remove()

删除具有指定值的项目。

charList =  ["a", "b", "c", "d"]
charList.remove('d')
print (charList) # ['a', 'b', 'c']

9.10. reverse()

颠倒列表中项目的顺序。

charList =  ["a", "b", "c", "d"]
charList.reverse()
print (charList) # ['d', 'c', 'b', 'a']

9.11. sort()

默认情况下,以升序对给定列表进行排序。

charList =  ["a", "c", "b", "d"]
charList.sort()
print (charList) # ["a", "b", "c", "d"]

学习愉快!

(Python基础教程之八)Python中的list操作的更多相关文章

  1. 《python基础教程》笔记之 序列通用操作

    索引 序列中的所有元素都是有编号的--从0开始递增.使用负数索引时,Python会从右边,也就是从最后一个元素开始计数,最后一个元素的位置编号是-1.此外,字符串是一个有字符组成的序列,字符串字面值可 ...

  2. (Python基础教程之十三)Python中使用httplib2 – HTTP GET和POST示例

    Python基础教程 在SublimeEditor中配置Python环境 Python代码中添加注释 Python中的变量的使用 Python中的数据类型 Python中的关键字 Python字符串操 ...

  3. (Python基础教程之十二)Python读写CSV文件

    Python基础教程 在SublimeEditor中配置Python环境 Python代码中添加注释 Python中的变量的使用 Python中的数据类型 Python中的关键字 Python字符串操 ...

  4. (Python基础教程之二十二)爬虫下载网页视频(video blob)

    Python基础教程 在SublimeEditor中配置Python环境 Python代码中添加注释 Python中的变量的使用 Python中的数据类型 Python中的关键字 Python字符串操 ...

  5. 改写《python基础教程》中的一个例子

    一.前言 初学python,看<python基础教程>,第20章实现了将文本转化成html的功能.由于本人之前有DIY一个markdown转html的算法,所以对这个例子有兴趣.可仔细一看 ...

  6. .Net程序员之Python基础教程学习----列表和元组 [First Day]

    一. 通用序列操作: 其实对于列表,元组 都属于序列化数据,可以通过下表来访问的.下面就来看看序列的基本操作吧. 1.1 索引: 序列中的所有元素的下标是从0开始递增的. 如果索引的长度的是N,那么所 ...

  7. python基础教程笔记—即时标记(详解)

    最近一直在学习python,语法部分差不多看完了,想写一写python基础教程后面的第一个项目.因为我在网上看到的别人的博客讲解都并不是特别详细,仅仅是贴一下代码,书上内容照搬一下,对于当时刚学习py ...

  8. python基础教程

    转自:http://www.cnblogs.com/vamei/archive/2012/09/13/2682778.html Python快速教程 作者:Vamei 出处:http://www.cn ...

  9. python基础教程(一)

    之所以选择py交易有以下几点:1.python是胶水语言(跨平台),2.python无所不能(除了底层),3.python编写方便(notepad++等文本编辑器就能搞事情),4.渗透方面很多脚本都是 ...

随机推荐

  1. 异常体系结构 throwable

    package com.yhqtv.demo01Exception; /* * 一.异常体系结构 *java.lang.Throwable * ------java.lang.Error:一般不编写针 ...

  2. Jquery中 $.Ajax() 参数详解

    1.url:要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type:要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如pu ...

  3. 14个快捷键让你的idea飞起来(新手向 + 演示)

    本期盘点一下博主在工作中,常用的13个idea快捷键,这些快捷键基本涵盖了大部分的开发场景,希望可以萌新们的idea使用效率,系统为mac系统 上一步 / 下一步撤销 / 反撤销进入一个类生成方法变量 ...

  4. NTSTATUS code 和 Windows 系统错误码 的对应关系

    出处:https://github.com/dokan-dev/dokany/blob/master/dokan/ntstatus.i case EPT_S_CANT_CREATE: return E ...

  5. 事件总线功能库,Reface.EventBus 详细使用教程

    Reface.AppStarter 中的事件总线功能是通过 Reface.EventBus 提供的. 参考文章 : Reface.AppStarter 框架初探 使用 Reface.EventBus ...

  6. 听说你在从事前端开发?那这10个JavaScript的优化问题你不得不知道!

    JavaScript的高效优化一直都是我们前端开发中非常重要的工作,也是很多开发人员无法做好的一部分内容,今天我总结了10个优化问题,大家可以参考来做优化,这其中很多问题都是大家经常遇到的哦. ==和 ...

  7. Android Resourse

    为什么80%的码农都做不了架构师?>>>   使用情景: 实现帧动画步骤的控制,这样动态的获取Drawable资源对应的R id,播放到那一步就加载到哪一步 private void ...

  8. Waiting for another flutter command to release the startup lock...

    2019独角兽企业重金招聘Python工程师标准>>> rm ./flutter/bin/cache/lockfile info from 转载于:https://my.oschin ...

  9. 演示:配置日志发送到syslog日志服务器

    演示目标:配置网络环境中的交换机和路由器将日志发送到syslog日志服务器. 演示环境:如下图10.54所示的演示环境. 演示背景:要求部署网络中的syslog服务器,集中的收集交换机S1和路由器R1 ...

  10. 如何将MAC的 Terminal 行首变得清爽简洁一点?

    作为一位开发人员,MAC带给我们更好的编程体验,Terminal也是经常会去操作的东西,但是说实话,默认的 Terminal 的各种设置,真的让我好难受 刚开始打开,可能看到的会是这样的,行首一大堆东 ...