Pythonリストindex()メソッド
例
値「チェリー」の位置は何ですか:
fruits = ['apple', 'banana', 'cherry']
x = fruits.index("cherry")
定義と使用法
このindex()
メソッドは、指定された値が最初に出現した位置を返します。
構文
list.index(elmnt)
パラメータ値
Parameter | Description |
---|---|
elmnt | Required. Any type (string, number, list, etc.). The element to search for |
その他の例
例
値32の位置は何ですか:
fruits = [4,
55, 64, 32, 16, 32]
x =
fruits.index(32)
注:このindex()
メソッドは、値の最初の出現のみを返します。