「Python」index()関数で配列の要素の位置を検索する

2021年12月2日

書式
リスト名 = [xxx]
リスト名.index(数字)
index()を使用して、配列に指定要素の位置を検索します。
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#リスト名cftLstの宣言
cftLst = [3, 4, 5, 3, 3,9]
print("配列cftLstに数字3のインデクス")
print(cftLst.index(3))
print("配列cftLstに数字9のインデクス")
print(cftLst.index(9))
#リスト名cftLstの宣言 cftLst = [3, 4, 5, 3, 3,9] print("配列cftLstに数字3のインデクス") print(cftLst.index(3)) print("配列cftLstに数字9のインデクス") print(cftLst.index(9))
#リスト名cftLstの宣言
cftLst = [3, 4, 5, 3, 3,9]
 
print("配列cftLstに数字3のインデクス")
print(cftLst.index(3))

print("配列cftLstに数字9のインデクス")
print(cftLst.index(9))

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
配列cftLstに数字3のインデクス
0
配列cftLstに数字9のインデクス
5
配列cftLstに数字3のインデクス 0 配列cftLstに数字9のインデクス 5
配列cftLstに数字3のインデクス
0
配列cftLstに数字9のインデクス
5

 

Python

Posted by arkgame