列表
列表的创建
列表的创建需要使用中括号[]。元素之间使用英文的逗号进行分割。
列表的创建方式:
(1)使用中括号
lst=['html','css']
(2)调用内置函数list()
lst2=list(['html','css'])
列表的特点
列表元素按顺序有序排序
索引映射唯一个元素
列表可以存储重复元素
任意数据类型混存
根据需要动态分配和回收内存
列表的查询操作
获取列表中指定元素的索引
index()函数
(1)如果查列表中存在N个相同的元素,只返回相同元素中的第一个元素的索引。
如:
a=list(['hello',1,2,3,4,5,1,2])
print(a.index(1))
结果如下
1
(2)如果查询的元素在列表中不存在,则会抛出ValueError。
a=list(['hello',1,2,3,4,5,1,2])
print(a.index(14))
结果抛出错误
ValueError: 14 is not in list
(3)还可以在指定的start和stop之间进行查找。
a=list(['hello',1,2,3,4,5,1,2])
print(a.index('hello',0,3))#指定查找下标范围为0-3,但不包括3.
0
获取列表中的单个元素
获取单个元素
正向索引从0到n-1
逆向索引从==-n到-1==
指定索引不存在,抛出indexError.
a=list(['hello','world',1,2,3,4])
print(a[1])
print(a[-1])
world
4
获取列表中的多个元素
- 语法格式
列表名[start:stop:step]
切片操作
切片的结果 | 原列表片段的拷贝 |
切片的范围 | 【start:stop】记住区间是左闭右开 |
step默认为1 | 简写为【start:stop】 |
step为正数 | 【stop:step】 切片的第一个元素默认为是列表的第一个元素 从start开始往后计算切片 |
step为正数 | 【start:step】 切片的最后一个元素默认是列表的最后一个元素 从start开始往后计算切片 |
step为负数 | 【stop:step】 切片的第一个元素默认为是列表的最后一个元素 从start开始往前计算切片 |
step为负数 | 【start:step】 切片的最后一个元素默认为是列表的第一个元素 从start开始往前计算切片 |
a=list(['hello','world',1,2,3,4,5,6,7,8,9,10])
print(a[0:4:1])
['hello', 'world', 1, 2]
元素 in 列表名
元素 not in 列表名
a=list(['hello','world',1,2,3,4,5,6,7,8,9,10])
print('hello' in a)
True
for 迭代变量 in 列表名:
操作
a=list(['hello','world',1,2,3,4,5,6,7,8,9,10])
for i in a:
print(i)
hello
world
1
2
3
4
5
6
7
8
9
10
列表元素的增加操作
append() | 在列表的末尾添加一个元素 |
extend() | 在列表的末尾至少添加一个元素 |
insert() | 在列表的任意位置添加一个元素 |
切片 | 在列表的任意位置添加至少一个元素 |
append用法
列表名.append(要添加的元素)
例子
a=list(['hello','world',1,2,3,4,5,6,7,8,9,10])
a.append(100)
print(a)
['hello', 'world', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100]
如果被添加元素是列表的话,会把这个列表当作一个元素。
a=list(['hello','world',1,2,3,4,5,6,7,8,9,10])
b=list(['i','you'])
a.append(b)
print(a)
['hello', 'world', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ['i', 'you']]
extend()用法
向列表的末尾一次性添加多个元素
列表名.extend(要添加的元素)
a=list(['hello','world',1,2,3,4,5,6,7,8,9,10])
b=list(['i','you'])
a.extend(b)
print(a)
['hello', 'world', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'i', 'you']
insert()用法
列表名.insert(索引位置,要添加的元素)
a=list(['hello','world',1,2,3,4,5,6,7,8,9,10])
a.insert(0,1)
print(a)
[1, 'hello', 'world', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
切片用法
a=list(['hello','world',1,2,3,4,5,6,7,8,9,10])
b=list([1,2,3,4,5])
a[1:]=b
print(a)
['hello', 1, 2, 3, 4, 5]
可以发现,索引为1(包括1)之后的元素被切除了,填入了b列表的元素。
列表的删除操作
remove()
特点:
- 一次删除一个元素
- 重复元素只删除第一个
- 元素不存在时抛出ValueError
pop()
特点:
- 删除一个指定索引位置上的元素
- 指定索引不存在抛出IndexError
- 不指定索引,删除列表中的最后一个元素
切片
特点:
一次至少删除一个元素
clear()
清空列表
del
删除列表
remove()用法
a=list(['hello','world',1,2,3,4,5,6,7,8,9,10])
a.remove(1)
print(a)
['hello', 'world', 2, 3, 4, 5, 6, 7, 8, 9, 10]
pop用法
a=list(['hello','world',1,2,3,4,5,6,7,8,9,10])
a.pop(1)
print(a)
['hello', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
切片操作
#切片操作,删除至少一个元素,将产生一个新的列表对象
a=list(['hello','world',1,2,3,4,5,6,7,8,9,10])
new_list=a[1:3]
print(a)
print(new_list)
原列表['hello', 'world', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
切片后的新列表['world', 1]
#不产生新的列表对象,而是删除原列表中的内容
a=list(['hello','world',1,2,3,4,5,6,7,8,9,10])
a[1:3]=[]
print(a)
['hello', 2, 3, 4, 5, 6, 7, 8, 9, 10]
clear()
#清除列表中的所有元素
a=list(['hello','world',1,2,3,4,5,6,7,8,9,10])
a.clear()
print(a)
[]
del
a=list(['hello','world',1,2,3,4,5,6,7,8,9,10])
del lst
列表元素的修改操作
- 为指定索引的元素赋予一个新值
- 为指定的切片赋予一个新值
#指定索引的元素赋予一个新值
a=list(['hello','world',1,2,3,4,5,6,7,8,9,10])
a[1]='you'
print(a)
['hello', 'you', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
#为指定切片的赋予一个新值
a=list(['hello','world',1,2,3,4,5,6,7,8,9,10])
a[2:4]=[3,4]
print(a)
#注意,这里是删除索引2到4(不包括4)的元素,在把后面的内容填入列表
['hello', 'world', 3, 4, 3, 4, 5, 6, 7, 8, 9, 10]
比如这个例子
a=list(['hello','world',1,2,3,4,5,6,7,8,9,10])
a[1:11]=['a','b','c']
print(a)
['hello', 'a', 'b', 'c', 10]
列表的排序操作
常见的两种方式
- 调用sort()方法,列表中的所有元素默认按照从小到大的顺序排序,可以指定reverse=True,进行降序排序
- 调用内置函数sorted(),可以指定reverse=True,进行降序排序,原列表不发生改变。
#调用sort()方法,默认为按照升序,注意列表元素的类型要一致
a=list([1,3,2,4,7,6,9,8,10,5])
a.sort()
print(a)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
#调用内置函数sorted(),将产生一个新的列表对象
a=list([1,3,2,4,7,6,9,8,10,5])
new_list=sorted(a)
print(new_list)
print(a)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 3, 2, 4, 7, 6, 9, 8, 10, 5]
列表生成式
列表生成式简称“生成列表的公式”
语法格式
[i*i for i in range(1,10)]
i*i 表示列表元素的表达式
i 自定义变量
range(1,10) 可迭代对象
注意事项:“表示列表元素的表达式”中通常包含自定义变量
b=list([i*i for i in range(1,10)])
print(b)
[1, 4, 9, 16, 25, 36, 49, 64, 81]
#列表中的元素为2 4 6 8 10
b=list([i*2 for i in range(1,6)])
print(b)
[2, 4, 6, 8, 10]