获取列表中指定元素的索引
index:
如查列表中存在N个相同元素,只返回相同元素中的第一个元素的索引
如果查询的元素在列表中不存在,则会抛出ValueError
还可以在指定的start和stop之间进行查找
获取列表中的单个元素
获取单个元素
正向索引从0到N-1 举例:lst[0]
逆向索引从-N到-1 举例:lst[-N]
指定索引不存,抛出indexError
lst=["hello","world","98",'hello']
print(lst.index('hello')) #如果列表中有相同元素只返回列表中相同元素的第一个元素的索引
#print(lst.index('python')) #ValueError: 'python' is not in list
#print(lst.index('hello',1,3)) #ValueError: 'hello' is not in list
print(lst.index('hello',1,4)) #3
#结果
0
ValueError: 'python' is not in list
ValueError: 'hello' is not in list
3
lst=['hello','world',98,'hello','world',123]
#获取索引为2的元素
print(lst[2]) #98
#获取索引-3的元素
print(lst[-3]) #hello
#获取索引为10的元素
print(lst[10]) #IndexError: list index out of range列表不在范围内
获取列表中的多个元素
语法格式:
列表名[start:stop:step]
lst=[10,20,30,40,50,60,70,80]
#start=1,stop=6,step1
print(lst[1:6:1])
print('原列表',id(lst))
lst2=lst[1:6:1]
print('切的片段:',id(lst2))
print(lst[1:6]) #默认步长为1
print(lst[1:6:2])
print(lst[:6:2])
#start=1,step=2,stop采用默认
print(lst[1::2])
#结果
[20, 30, 40, 50, 60]
原列表 2478581740160
切的片段: 2478581706816
[20, 30, 40, 50, 60]
[20, 40, 60]
[10, 30, 50]
[20, 40, 60, 80]
lst=[10,20,30,40,50,60,70,80]
#start=1,stop=6,step1
print(lst[1:6:1])
print('原列表',id(lst))
lst2=lst[1:6:1]
print('切的片段:',id(lst2))
print(lst[1:6]) #默认步长为1
print(lst[1:6:2])
print(lst[:6:2])
#start=1,step=2,stop采用默认
print(lst[1::2])
print('-----step步长为负数的情况-----')
print('原列表:',lst)
print(lst[::-1])
#start=7,stop 省略 step=-1
print(lst[7::-1])
#start=6,stop=0,step=-2
print(lst[6:0:-2])
#结果
[20, 30, 40, 50, 60]
原列表 2499609031296
切的片段: 2499608997952
[20, 30, 40, 50, 60]
[20, 40, 60]
[10, 30, 50]
[20, 40, 60, 80]
-----step步长为负数的情况-----
原列表: [10, 20, 30, 40, 50, 60, 70, 80]
[80, 70, 60, 50, 40, 30, 20, 10]
[80, 70, 60, 50, 40, 30, 20, 10]
[70, 50, 30]
增加操作:append() 在列表的末尾添加一个元素
extend() 在列表的末尾至少添加一个元素
insert() 在列表的任意位置添加至少一个元素
切片 在列表的任意位置添加至少一个元素
# 向列表的末尾添加一个元素
lst=[10,20,30]
print('添加元素之前',lst,id(lst))
lst.append(100) #append追加
print('添加元素之后',lst,id(lst))
lst2=['hello','world']
lst.append(lst2)
print(lst) #将lst2做为一个元素添加到列表的末尾
#向列表的末尾一次性添加多个元素
lst.extend(lst2)
print(lst)
# 在任意位置上添加一个元素
lst.insert(1,90) #在指定1的位置追加90
print(lst)
lst3=[True,False,'hello']
#在任意的位置上添加N多个元素
lst[1:]=lst3 #从1的位置上切 把lst3放进来
print(lst)
#结果
添加元素之前 [10, 20, 30] 2041786266240
添加元素之后 [10, 20, 30, 100] 2041786266240
[10, 20, 30, 100, ['hello', 'world']]
[10, 20, 30, 100, ['hello', 'world'], 'hello', 'world']
[10, 90, 20, 30, 100, ['hello', 'world'], 'hello', 'world']
[10, True, False, 'hello']
删除操作:
remove() 一次删除一个元素
重复元素删除第一个
元素不存在抛出ValueError
pop() 删除一个指定索引位置上的元素
指定索引不存在抛出IndexError
不指定索引,删除列表中最后一个元素
切片:一次至少删除一个元素
clear() 清空列表
del 删除列表
lst=[10,20,30,40,50,60,30]
lst.remove(30) # 从列表中移除一个元素,如果有重复元素只移除第一个元素
print(lst)
#lst.remove(100) # 报错提示:列表里没有这个元素
#pop()根据索引移除元素
lst.pop(1) #在指定的1位置移除
print(lst)
#lst.pop(5) #如果指定的索引位置不存在,将抛出异常
lst.pop() #如果不指定参数(索引),将删除列表中的最后一个元素
print(lst)
print('----切片操作-删除至少一个元素,将产生一个新的列表对象----')
new_list=lst[1:3]
print('原列表',lst)
print('切片后的列表',new_list)
"""不产生新的列表对象,而是删除列表中的内容"""
lst[1:3]=[]
print(lst)
"""清除列表中的所有元素"""
lst.clear()
print(lst)
""" del语句将列表对象删除"""
del lst #都删除了 所以会报错
#print(lst) #NameError: name 'lst' is not defined.
#结果
[10, 20, 40, 50, 60, 30]
[10, 40, 50, 60, 30]
[10, 40, 50, 60]
----切片操作-删除至少一个元素,将产生一个新的列表对象----
原列表 [10, 40, 50, 60]
切片后的列表 [40, 50]
[10, 60]
[]
为指定索引的元素赋予一个新值
为指定的切片赋予一个新值
lst=[10,20,30,40]
#一次修改一个值
lst[2]=100
print(lst)
lst[1:3]=[300,400,500,600] #默认索引1不包括3
print(lst)
#结果
[10, 20, 100, 40]
[10, 300, 400, 500, 600, 40]
常见的两种方式
调用sort()方法,列有中的所有元素默认按照从小到大的顺序进行排序,可以指定reverse=True,进行降序排序
调用内置函数sorted(),可以指定reverse=True,进行降序排序,原列表不发生改变
lst=[20,40,10,98,54]
print('排序前的列表',lst)
#开始排序,调用列表对象的sort方法,升序排序
lst.sort()
print('排序后的列表',lst,id(lst))
#通过指定关键字参数,将列表中的元素进行降序排序
lst.sort(reverse=True) #reverse=True 表示降序排序,reverse=False就是升序
print(lst)
lst.sort(reverse=False) #reverse=True 表示降序排序,reverse=False就是升序
print(lst)
print('----使用内置函数sorted()对列表进行排序,将产生一个新的列表对象----')
lst=[20,40,10,98,54]
print('原列表',lst)
#开始排序
new_list=sorted(lst)
print(lst)
print(new_list)
#指定关键字参数,实现列表元素的降序排序
desc_list=sorted(lst,reverse=True)
print(lst)
print(desc_list)
#结果
排序前的列表 [20, 40, 10, 98, 54]
排序后的列表 [10, 20, 40, 54, 98] 2063690953344
[98, 54, 40, 20, 10]
[10, 20, 40, 54, 98]
----使用内置函数sorted()对列表进行排序,将产生一个新的列表对象----
原列表 [20, 40, 10, 98, 54]
[20, 40, 10, 98, 54]
[10, 20, 40, 54, 98]
[20, 40, 10, 98, 54]
[98, 54, 40, 20, 10]