列表是什么
- 列表是处理一组有序项目的数据结构
- 列表是python中最具有灵活性的有序集合对象类型
- 列表可以包含任何类型的对象:字符串、数字甚至其他列表
- 列表是可变的数据类型,即这种类型的值是可以被修改的
举例: 定义一个 dota2 选手名字清单
player_list = ['ameame', 'maybe', 'chalice', 'fy', 'xnova', 'faith-bian','y`','nothingtosay','xinq']
列表的定义
- 列表通过方括号中用逗号分割的项目定义
- 定义定义空列表
- 方法 1
list1 = []
print(list1)
- 方法 2
list2 = list()
print(list2)
- 列表中可以装入python的任何对象
list_many = [
1,
3.14,
'wangchunyu',
True,
[1, 2],# 列表
(1, 2),# 元组
{3, 4},# 集合
{'key1':'value1','key2':'value2'},# 字典
None,
]
列表的基本操作
- 索引
列表可以通过下标访问列表中的元素,分为正向下标和负向下标(正索引和负索引)
正索引:下标从零开始计数,0表示第一个元素,1表示第二个元素,以此类推;
负索引:下标从 -1 开始计数,-1 表示的是最后一个元素,-2 表示的是倒数第二个元素,以此类推。
- 尝试这个实例,更好的理解下标的意思
list1 = ['ameame', 'maybe', 'chalice', 'fy', 'xnova', 'faith-bian','y`','nothingtosay','xinq']
print(list1[0])
print(list1[3])
print(list1[4])
print(list1[-1])
print(list1[-5])
print(list1[-100])
- 这里注意,下标不能越界,不然报错:
IndexError:list index out of range
- 切片
切片是通过下标访问列表中的元素,切片可以取出一个子列表
格式:list_example = [start:end:step]
start - 开始元素下标;end - 结束元素下标;step - 步长。
这里要注意,起止区间是左闭右开的,即:[start:end)
,即取的区间不包括结束元素。
参数 | 默认值 |
---|---|
step | 默认为 1 |
start | step为正,默认为0;反之默认为-1 |
end | step为正,默认为列表结尾;反之为列表开头 |
list1 = ['ameame', 'maybe', 'chalice', 'fy', 'xnova', 'faith-bian','y`','nothingtosay','xinq']
print(list1[0:3])
# ['ameame', 'maybe', 'chalice']
print(list1[0:8:2])
# ['ameame', 'chalice', 'xnova', 'y`']
print(list1[2::])
# ['chalice', 'fy', 'xnova', 'faith-bian', 'y`', 'nothingtosay', 'xinq']
print(list1[:5])
# ['ameame', 'maybe', 'chalice', 'fy', 'xnova']
print(list1[:])
# ['ameame', 'maybe', 'chalice', 'fy', 'xnova', 'faith-bian', 'y`', 'nothingtosay', 'xinq']
print(list1[::])
# ['ameame', 'maybe', 'chalice', 'fy', 'xnova', 'faith-bian', 'y`', 'nothingtosay', 'xinq']
- 增加
增加列表元素的方式
方式 | 作用 |
---|---|
append | 在末尾,作为整体追加,类型不限 |
insert | 在index位置元素前面插入元素 |
extend | 扩展列表 |
示例:
- append 的用法
list1 = ['1', '2', '3', '4', '5']
list2 = ['a', 'b', 'c']
list2.append(1)
print(list2)
# ['a', 'b', 'c', 1]
list2.append('I am Ameame')
print(list2)
# ['a', 'b', 'c', 1, 'I am Ameame']
list2.append(list1)
print(list2)
# ['a', 'b', 'c', 1, 'I am Ameame', ['1', '2', '3', '4', '5']]
- insert 的用法
list3 = [1, 2, 3]
list3.insert(1,'?')
print(list3)
# [1, '?', 2, 3]
list3.insert(-1, '!')
print(list3)
# [1, '?', 2, '!', 3]
- extend 的用法 这里补充一下 + 的用法 两者有点类似
- 但是 extend 只限可以转化为list的类型(str,list,tuple)
- + 只能是 list + list
list4 = [0,1,2,3]
list5 = ['a','b','c']
list4.extend(list5)
print(list4)
# [0, 1, 2, 3, 'a', 'b', 'c']
list6 = [0,1,2,3]
print(list6+list5)
# [0, 1, 2, 3, 'a', 'b', 'c']
tuple1 = (9,8,7)
list6.extend(tuple1)
print(list6)
# [0, 1, 2, 3, 9, 8, 7]
str1 = '??emo??'
list7 = [1,2,3,4]
list7.extend(str1)
print(list7)
# [1, 2, 3, 4, '?', '?', 'e', 'm', 'o', '?', '?']
print(list7+str1)
'''
Traceback (most recent call last):
File "<pyshell#36>", line 1, in <module>
print(list7+str1)
TypeError: can only concatenate list (not "str") to list
'''
- 删除
删除列表元素的方式
方式 | 作用 |
---|---|
pop | 删除元素,默认删除最后一个(可以指定index删除) |
remove | 指定删除的数据 |
del | 根据下标删除 |
clear | 清空 |
示例:
- pop 用法
list1 = [1,2,3,4]
# 注意 pop 方法可以产生一个返回值,返回值是被删除的元素
t = list1.pop()
print(t, list1)
# 4 [1, 2, 3]
t = list1.pop(1)
print(t, list1)
# 2 [1, 3]
- remove 用法
- 指定删除的元素,会删除匹配到的第一个元素,如果元素不存在的时候会报错
list1 = ['ameame', 'maybe', 'chalice', 'fy', 'xnova']
list1.remove('ameame')
print(list1)
# ['maybe', 'chalice', 'fy', 'xnova']
list1 = ['ameame', 'maybe', 'chalice', 'fy', 'xnova', 'ameame']
list1.remove('ameame')
print(list1)
# ['maybe', 'chalice', 'fy', 'xnova', 'ameame']
- del 用法
- del 是 python 中的一个关键字,用户删除变量(的引用)
list1 = ['ameame', 'maybe', 'chalice', 'fy', 'xnova', 'faith-bian','y`','nothingtosay','xinq']
del list1[0]
print(list1)
# ['maybe', 'chalice', 'fy', 'xnova', 'faith-bian', 'y`', 'nothingtosay', 'xinq']
del list1[4:]
print(list1)
# ['maybe', 'chalice', 'fy', 'xnova']
- clear 用法
list1 = ['ameame', 'maybe', 'chalice', 'fy', 'xnova', 'faith-bian','y`','nothingtosay','xinq']
list1.clear()
print(list1)
# []
- 修改
修改元素主要是利用下标和切片来增加或修改、删除
示例:
list1 = ['ameame', 'maybe', 'chalice', 'fy', 'xnova', 'faith-bian','y`','nothingtosay','xinq']
list1[0] = '被T了'
print(list1)
# ['被T了', 'maybe', 'chalice', 'fy', 'xnova', 'faith-bian', 'y`', 'nothingtosay', 'xinq']
list1[4:] = ['oldeleven']
print(list1)
# ['被T了', 'maybe', 'chalice', 'fy', 'oldeleven']
- 反转
反转方法: reverse 或 切片
list1 = ['ameame', 'maybe', 'chalice', 'fy', 'xnova']
- 切片
- 切片不会影响原列表
print(list1[::-1])
# ['xnova', 'fy', 'chalice', 'maybe', 'ameame']
print(list1)
# ['ameame', 'maybe', 'chalice', 'fy', 'xnova']
- reverse 会影响原列表
print(list1.reverse())
# None
print(list1)
# ['xnova', 'fy', 'chalice', 'maybe', 'ameame']
- 排序
列表排序 - sort
list1 = ['ameame', 'maybe', 'chalice', 'fy', 'xnova']
print(list1.sort())
# None
print(list1)
# ['ameame', 'chalice', 'fy', 'maybe', 'xnova']
- sort 是按照首字母的 acsii 码进行排序
- 列表里不能存在多种不同类型的数据 不然会报错
- 统计 && 查下标
len、count、index
list1 = ['ameame', 'maybe', 'chalice', 'fy', 'xnova']
- 统计列表的长度
print(len(list1))
# 5
- 统计元素出现的次数
print(list1.count('ameame'))
# 1
- 查找元素首次出现的位置参数
print(list1.index('ameame'))
# 0
print(list1.index('fy'))
# 3