Python list列表index查找使用方法,pythonlist,本文主要讲列表的inde
Python list列表index查找使用方法,pythonlist,本文主要讲列表的inde
本文主要讲列表的index方法,当你需要知道某个值在Python列表list中的索引位置时候,你可以使用index()方法,其返回值是要查值的索引。
pyton list index()方法标准格式
index()方法和列表或是其它python内置数据类型方法的调用格式一样:变量名.方法(参数),这个格式是方法调用的标准格式。
查找演示操作
>>> a = ['www','iplaypython','com']
>>> a.index('iplaypython')
1
#新建设变量名a的列表
#查找字符串‘iplaypython’在列表a中的位置
#返回1,表示要查找参数在列表中的索引下标位置是1
再来查找一个不存在的参数,看看会有什么样结果
>>> a.index('hello')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: 'hello' is not in list
当查找字符串‘iplaypython’时,返回了它的索引位置;而查找'hello'时则引发了一个错误异常,并报错说'hello' is not in list,字符串'hello'不在列表中。
总结
用列表的index()方法查找参数时:如果参数存在,返回索引位置;如果参数不存在,则报错。
浏览此文章用户还在关注:list.append
编橙之家文章,
相关内容
- Python 列表list的append添加方法,pythonappend,Python列表app
- Python列表删除 pop() 方法返回指定对象,pythonpop,列表的
- Python 列表推导式 格式及使用方法,python推导,初学者可
- Python list列表extend()使用方法,pythonextend,Python列表的基
- Python for循环控制语句一般格式及方法,pythonfor,for循环
- Python 字典items返回列表,iteritems返回迭代器,返回列表
- Python else与elif语句语法讲解,pythonelif,编橙之家本文对
- Python global全局变量语句使用方法,pythonglobal,编橙之家
- Python 列表count()函数元素次数统计,pythoncount,如何查看
- Python SyntaxError语法错误原因及解决,pythonsyntaxerror,Pyt
评论关闭