列表的查询操作
获取列表中指定元素的索引
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
48.获取列表中指定的元素
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列表不在范围内
49.获取列表中的多元素_切片操作
获取列表中的多个元素