Python3-list-index
提供:Dev Guides
Python 3-リストindex()メソッド
説明
- index()*メソッドは、objが現れるリスト内の最低のインデックスを返します。
構文
以下は* index()*メソッドの構文です-
list.index(obj)
パラメーター
*obj* -これは見つけられるべきオブジェクトです。
戻り値
このメソッドは、見つかったオブジェクトのインデックスを返します。それ以外の場合は、値が見つからないことを示す例外が発生します。
例
次の例は、index()メソッドの使用方法を示しています。
#!/usr/bin/python3
list1 = ['physics', 'chemistry', 'maths']
print ('Index of chemistry', list1.index('chemistry'))
print ('Index of C#', list1.index('C#'))
結果
上記のプログラムを実行すると、次の結果が生成されます-
Index of chemistry 1
Traceback (most recent call last):
File "test.py", line 3, in <module>
print ('Index of C#', list1.index('C#'))
ValueError: 'C#' is not in list