を:私は(アレイ内の特定の要素を検索しようとしていますPythonで2次元配列を検索 - 私はPythonで、次の2次元配列(リストのリスト)を作成した最善の方法+インデントエラー
#creating a 2d array (3 rows by 7 columns) and populating it with numbers
matrix=[1,2,3,4,5,6,7],[8,9,10,11,12,13,14],[15,16,17,18,19,20,21]
rows=len(matrix) #finding the max number of rows in the matrix, in this case 3
columns=len(matrix[0]) #finding the max number of columns in each row, 7 in this case
)番号9を言うし、次に見つかった場合、「見つかった」、および「見つかりません」、そうでない場合、アレイに、次のコードで印刷:
number=int(input("What number are you looking for?"))
for i in range(rows):
for j in range(columns):
if matrix[i][j]==number:
print("Found it!")
break
else:
print("not found")
出力しかし、誤っている:
>>What number are you looking for? 9
>>Found it!
>>not found
私は2つの質問があります。1.誰かがこの問題を参照して識別情報を明確に説明でき、なぜ2番目の「見つからない」が常に出力されるのか? 2. numpyを使用しないでこれを行うより効率的な方法がありますか?
注:これは重複していません。私は他のエントリを検索しています。明示的に尋ねる
ここrepl.it: https://repl.it/IcJ3/3
誰かが以下のように答えを示唆した。(私はすでにこれをしようとした)
https://repl.it/IcJ3/5 注意、それはすべてのいずれかで動作しません:
number=int(input("What number are you looking for?"))
for i in range(rows):
for j in range(columns):
if matrix[i][j]==number:
print("Found it!")
break
else:
print("not found")
誤った出力、まだ!
What number are you looking for? 9
not found
not found
not found
not found
not found
not found
not found
not found
Found it!
not found
not found
not found
not found
not found
not found
not found
あなたの他にif文のインデントに一致しません。 – Zeokav
ここにはどんなステートメントがありますか? –
私は、他の人のために異なるポジション/アイデンティティで遊んでいます:....それは私が助けを必要とするものです。もし私が知っていたら、私は尋ねていないだろう!ありがとうございます事前に – MissComputing