如何理解python列表

发布时间:2021-10-09 13:37:07 作者:iii
来源:亿速云 阅读:110

本篇内容介绍了“如何理解python列表”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

目录

列表list

1、列表创建

list1 = [3.14, 1.61, 0, -9, 6]
list2 = [‘train', ‘bus', ‘car', ‘ship']
list3 = [‘a',200,‘b',150, ‘c',100]
list4 = [] #创建空列表

 在Python中,经常用到列表中的列表,即二维列表

2、列表访问

索引访问方式适用于所有序列类型的对象:列表、元组、字符串。

1)一维列表的访问
vehicle = [‘train', ‘bus', ‘car', ‘ship']
vehicle[0]
‘train'
vehicle[1]
‘bus'
vehicle[2]
‘car'
2)二维列表的访问

对二维列表中的元素进行访问,需要使用两对方括号来表示,第一个表示选择子列表,第二个在选中的子列表中再选择其元素。

computer=[[‘IBM',‘Apple',‘Lenovo'],[‘America',‘America',‘China']]
computer[0][-1]
‘Lenovo'
computer[1][2]
‘China'

3、修改元素

4、列表切片

1.在列表中,可以使用切片操作来选取指定位置上的元素组成新的列表。简单的切片方式为:

原列表名[start : end]

需要提供开始值start和结束值end作为切片的开始和结束索引边界。

开始值start索引位置上的元素是包含在切片内的,结束值end索引位置上的元素则不包括在切片内;

当切片的左索引start为0时可缺省,当右索引end为列表长度时也可缺省。

这个简单的切片操作从原列表中选取索引位于[start, end)区间内的元素组成新的列表。

2.切片操作也可以提供一个非零整数(即可正可负,但不能为0)作为索引值增长的步长step值。使用方式为:

原列表名[start : end : step]

5、del命令

vehicle = [‘train', ‘bus', ‘car', ‘ship']
del vehicle[3]
vehicle #删除了'ship'
[‘train', ‘bus', ‘car']
del vehicle[3] #超出索引范围
Traceback (most recent call last):
File “<pyshell#50>”, line 1, in
del vehicle[3]
IndexError: list assignment index out of range
del vehicle #删除列表vehicle
vehicle #列表vehicle不存在了
Traceback (most recent call last):
File “<pyshell#82>”, line 1, in
vehicle
NameError: name ‘vehicle' is not defined
另外,remove()、pop()、clear()方法均能实现列表元素的删除

6、列表运算

1)列表相加
vehicle1 = [‘train', ‘bus', ‘car', ‘ship']
vehicle2 = [‘subway', ‘bicycle']
vehicle1 + vehicle2
[‘train', ‘bus', ‘car', ‘ship', ‘subway', ‘bicycle']
vehicle1 # vehicle1没有改变
[‘train', ‘bus', ‘car', ‘ship']
vehicle2
[‘subway', ‘bicycle']
vehicle=vehicle1 + vehicle2 # 生成新列表赋值给变量vehicle
vehicle
[‘train', ‘bus', ‘car', ‘ship', ‘subway', ‘bicycle']
vehicle+=[‘bike'] #复合赋值语句
vehicle
[‘train', ‘bus', ‘car', ‘ship', ‘subway', ‘bicycle', ‘bike']
2)列表相乘
vehicle1 = [‘train', ‘bus']
vehicle1*2
[‘train', ‘bus', ‘train', ‘bus']
vehicle1 #原列表保持不变
[‘train', ‘bus']
vehicle=vehicle1*2 #赋值语句
vehicle
[‘train', ‘bus', ‘train', ‘bus']
vehicle*=2 #复合赋值语句
vehicle
[‘train', ‘bus', ‘train', ‘bus', ‘train', ‘bus', ‘train', ‘bus']

7、列表方法

列表中的方法可看作是作用于Python中列表这一特定类型对象的函数。

1) index(value[,start=0[,stop]])

index()方法用于从列表中找出与value值匹配的第一个元素索引位置。

如果没有指定参数start的值,则从索引为0的位置开始查找,否则从索引为strat的位置开始查找。

如果没有指定结束索引位置stop的值,可以查找到列表最后元素,否则在位于[start, stop)内的索引区间查找。

如果找不到匹配项,就会引发异常。

vehicle = [‘train', ‘bus', ‘car', ‘subway', ‘ship', ‘bicycle', ‘car']
vehicle.index(‘car') #整个列表范围内'car'第1次出现的索引位置是2
2
vehicle.index(‘plane') #整个列表范围内没有'plane'
Traceback (most recent call last):
File “<pyshell#81>”, line 1, in
vehicle.index(‘plane')
ValueError: ‘plane' is not in list
vehicle.index(‘plane') #整个列表范围内没有'plane'
Traceback (most recent call last):
File “<pyshell#81>”, line 1, in
vehicle.index(‘plane')
ValueError: ‘plane' is not in list
vehicle.index(‘plane') #整个列表范围内没有'plane'
Traceback (most recent call last):
File “<pyshell#81>”, line 1, in
vehicle.index(‘plane')
ValueError: ‘plane' is not in list
2) count()

count()方法,用于统计某个元素在列表中出现的次数。

vehicle = [‘train', ‘bus', ‘car', ‘subway', ‘ship', ‘bicycle', ‘car']
vehicle.count(‘car')
2
vehicle.count(‘bus')
1
vehicle.count(‘bike')
0
3)append()

append()方法,追加单个元素到列表的尾部,只接受一个元素,元素可以是任何数据类型,被追加的元素在列表中保持着原结构类型。

vehicle = [‘train', ‘bus', ‘car', ‘ship']
vehicle.append (‘plane') #追加一个元素'plane'
vehicle
[‘train', ‘bus', ‘car', ‘ship', ‘plane']
vehicle.append(8) #追加一个元素8
vehicle
[‘train', ‘bus', ‘car', ‘ship', ‘plane', 8]
vehicle.append([8,9]) #追加一个元素[8,9]
vehicle
[‘train', ‘bus', ‘car', ‘ship', ‘plane', 8, [8, 9]]
vehicle.append(10,11) #追加2个元素10和11,出错
Traceback (most recent call last):
File “<pyshell#7>”, line 1, in
vehicle.append(10,11)
TypeError: append() takes exactly one argument (2 given)
4)extend()

列表.extend()方法在列表的末尾追加该方法中参数容器(列表、元组、字符串、字典、集合)中的所有元素。如果参数为字典,则追加字典中的所有键(key)

5)insert()

insert()方法,将一个元素插入到列表中的指定位置。列表的insert方法有两个参数,第一个参数是索引点,即插入的位置,第二个参数是插入的元素。

“如何理解python列表”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

推荐阅读:
  1. python列表
  2. Python之列表

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python

上一篇:什么是python条件控制与循环控制

下一篇:Python基础的示例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》