3.列表
3.1 列表介绍
列表的特点:
- 列表元素按顺序有序排序
- 索引映射唯一个数据
- 列表可以存储重复数据
- 任意数据类型混存
- 根据需要动态分配和回收内存
list = ['hello','world',98]
print(id(list)) #3175868791232
print(type(list)) #<class 'list'>
print(id(list[0])) #2932505697904
print(type(list[0])) #<class 'str'>
print(list) #['hello', 'world', 98]
3.2 创建列表
list1 = ['hello','world',98]
#使用内置函数list()
list2 =list(['hello','world',98])
3.3 查询列表
- 获取列表中指定元素的索引index()
1.如查列表中存在N个相同元素。只返回相同元素中的第一个元素的索引
2.如果查询的元素在列表中不存在,则会抛出ValueError
3.还可以在指定的start和stop之间进行查找 - 获取列表中的单个元素
1.正向索引从0到N-1,举例: lst[0]
2.逆向索引从-N到-1,举例: lst[-N]
3.指定索引不存。抛出IndexError
list = ['hello','world','hello',98,90,0]
print(list.index('hello'))# 0
print(list.index('hello',1,4))# 2
print(list[2])# hello
print(list[-3])# 98
- 获取列表中的多个元素
list = ['hello','world','hello',98,90,0]
list1 = list[1:6:1]
print(list1)#['world', 'hello', 98, 90, 0]
print(list[1:5:2])#['world', 98]
print(list[1::2])#['world', 98, 0]
print(list[:5:2])#['hello', 'hello', 90]
print(list[::2])#['hello', 'hello', 90]
print(list[:5:])#['hello', 'world', 'hello', 98, 90]
print(list[5:0:-1])#[0, 90, 98, 'hello', 'world']
print(list[::-1]) #[0, 90, 98, 'hello', 'world', 'hello']
3.4 列表元素的查询操作
- 判断元素在列表中是否存在 in 或者 not in
- 列表的遍历
list = ['hello','world','hello',98,90,0]
print(id(list))#1668510382592
list.append(300)
print(list)#['hello', 'world', 'hello', 98, 90, 0, 300]
print(id(list))#1668510382592
3.5 列表元素的增加操作
- append() :在列表的末尾添加一个元素
#append() id不变,说明是在原来列表的基础上进行添加
list = ['hello','world','hello',98,90,0]
print(id(list))#1668510382592
list.append(300)
print(list)#['hello', 'world', 'hello', 98, 90, 0, 300]
print(id(list))#1668510382592
- extend() :在列表的末尾至少添加一个元素
# 向列表的末尾添加多个元素
#如果是append()
list = ['hello','world','hello',98,90,0]
list1 = ['my','test']
list.append(list1)
print(list) #['hello', 'world', 'hello', 98, 90, 0, 300, ['my', 'test']]
#如果是extend()
list = ['hello','world','hello',98,90,0]
list.extend(list1)
print(list) #['hello', 'world', 'hello', 98, 90, 0, 300, 'my', 'test']
- insert() : 在列表的任意位置添加一个元素
#在任意位置添加1个元素
list = ['hello','world','hello',98,90,0,300, 'my', 'test']
list.insert(1,90)
print(list) #['hello', 90, 'world', 'hello', 98, 90, 0, 300, 'my', 'test']
- 切片 :在列表的任意位置添加至少一个元素
#在任意位置添加N多个元素
list = ['hello','world','hello',98,90,0,300, 'my', 'test']
list[1:] = list1
print(list) #['hello', 'my', 'test']
3.6 列表元素的删除操作
- remove() :一次删除一个元素;重复元素只删除第一个;元素不存在抛出ValueError
- pop() :删除一个指定索引位置上的元素;指定索引不存在抛出IndexError;不指定索引删除列表中最后一个元素
- 切片 :一次至少删除一个元素
- clear() :清空列表
- del() :删除列表
# remove()
list = ['hello','world','hello',98,90,0]
list.remove(0)
print(list)# ['hello', 'world', 'hello', 98, 90]
# pop() 根据索引移除元素
list.pop(1)
print(list) #['world', 'hello', 98, 90]
# 切片删除至少一个元素
list2 = list[1:3]
print(list2)# ['hello', 98]
# 切片使列表为空,不产生新的列表,而是删除原有列表的部分内容
print(list)#['hello', 'hello', 98, 90]
list[1:3] = []
print(list)#['hello', 90]
# 清除列表中的所有元素
list.clear()
print(list)#[]
# del语句将列表对象删除
del list
print(list) #<class 'list'>
3.7 列表元素的修改操作
- 为指定索引的元素赋予一个新值
- 为指定的切片赋予一个新值
list = ['hello','world','hello',98,90,0]
#一次修改一个值
list[2]='wow' #['hello', 'world', 'wow', 98, 90, 0]
print(list)
#一次修改多个值
list[1:3]=[1,2,3]
print(list)#['hello', 1, 2, 3, 98, 90, 0]
3.8 列表元素的排序操作
- 调用sort()方法,列表中的元素按照从小到大的顺序排序,可以指定reverse=True,进行降序排序,列表id不变
- 调用内置函数sorted(),可以指定reverse =True,进行降序排序,列表id改变
#*****调用sort()方法*****
list = [1,9,3,4,5,2,3,4]
#升序排序 list可直接调用sort方法,不需要二次赋值
#!!!!如果 list1 = list.sort();print(list1)会输出None
list.sort()
print(list) #[1, 2, 3, 3, 4, 4, 5, 9]
print(id(list)) # 2150274759168
#通过指定关键字参数,将列表中的元素进行降序排序
list.sort(reverse=True)
print(list) #[9, 5, 4, 4, 3, 3, 2, 1]
print(id(list)) #2150274759168
#通过指定关键字参数,将列表中的元素进行升序排序
list.sort(reverse=False)
print(list) #[1, 2, 3, 3, 4, 4, 5, 9]
print(id(list)) #2150274759168
#*****调用内置函数sorted()方法*****
#list不能直接调用sorted方法
#!!!使用如list.sorted(list),会报错:'list' object has no attribute 'sorted'
list1 = sorted(list)
print(list) #[1, 2, 3, 3, 4, 4, 5, 9]
print(id(list1)) #2692470922752
list2 = sorted(list,reverse=True)
print(list2) #[9, 5, 4, 4, 3, 3, 2, 1]
print(id(list2)) #1316912946880
3.9 列表生成式(生成列表的公式
- 语法格式:[ i*i for i in range(1,10)]
- i:自定义变量;i*i:列表元素的表达式;range(1,10):可迭代对象
#输出2到10的偶数
list = [i*2 for i in range(1,6)]
print(list) #[2, 4, 6, 8, 10]