Python基础知识包括获取索引、获取特定元素以及进行切片操作等基础功能
发布时间
阅读量:
阅读量
列表查询操作分析
获取列表中特定元素的索引
index:
若列表中包含多个相同元素,仅返回首个匹配项的索引位置
当所查找的元素未在列表中出现时,系统将触发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
全部评论 (0)
还没有任何评论哟~
