2017-03-03 4 views
-2

2d配列のインデックスは、数字が左右の数字より大きく、上下の数字よりも小さいもの、またはその逆のものを探す必要があります。2dリスト内のインデックスを条件付きで検索する

私は2つのループのために1つの列のために1つが必要であることを知っています。私はxを与えられ、インデックスを見つけるために2次元配列の数字と比較する必要があります。

list = [[1,4,6,7,8], 
     [2,4,6,7,8], 
     [1,4,6,7,8], 
     [1,4,6,7,8], 
     [2,4,6,7,8], 
     [6,4,6,7,8]] 

私は2Dリストにインデックスを見つける必要があるxと私は他の数字がより大きいか4未満ででき、その逆の右、上下、左右にWHEREまたは4を与えられていた場合誰かが解決策にそれと

list = [[1, 4, 6, 7, 8], 
     [2, 4, 6, 7, 8], 
     [1, 4, 6, 7, 8], 
     [1, 4, 6, 7, 8], 
     [2, 4, 6, 7, 8], 
     [6, 4, 6, 7, 8]] 

x = [x for x in list if 4 in x][0] 
print('The index is (%d,%d)' % (list.index(x), x.index(4))) 

を提供してくださいしようとし、それは私だけ最初のインデックスを与えるだろうが、私は配列全体をチェックし、問題がより少ないより多いかどうかを確認するif文を使用する必要があります。

+5

宿題サービスではありませんが、試行錯誤して解決策を試してください。 – ospahiu

+0

質問に追加されました – mcfellows

答えて

0

これは何だろう...

x = int(input('Enter x: ')) 
data = [[1, 4, 6, 7, 8], 
     [2, 4, 6, 7, 8], 
     [1, 4, 6, 7, 8], 
     [1, 4, 6, 7, 8], 
     [2, 4, 6, 7, 8], 
     [6, 4, 6, 7, 8]] 

dataTranspose = list(zip(*data)) 

for each_row_number in range(0, len(data)): 

    tempRow = list(enumerate(data[each_row_number])) 
    maxIndex, maxVal = max(tempRow, key=lambda eachPair: eachPair[1]) 
    minIndex, minVal = min(tempRow, key=lambda eachPair: eachPair[1]) 

    if x == maxVal: 
     if x == min(dataTranspose[maxIndex]): 
      print(' found another at: (' + str(each_row_number) + ', ' + str(maxIndex) + ')') 
    elif x == minVal: 
     if x == max(dataTranspose[minIndex]): 
      print(' found another at: (' + str(each_row_number) + ', ' + str(minIndex) + ')') 

例を実行します?

Enter x: 8 
found another at: (0, 4) 
found another at: (1, 4) 
found another at: (2, 4) 
found another at: (3, 4) 
found another at: (4, 4) 
found another at: (5, 4) 

例は、実行します。

Enter x: 4 
found another at: (5, 1) 

例は、実行します。

Enter x: 6 

(この第3の例では、x = 6の結果は得られず、与えられた条件のいずれかが2dリストのどこでも満足しない)。

+0

おかげでたくさんのtkhurana96が私の探しているものになります – mcfellows

+0

@mcfellows素晴らしい – tkhurana96

関連する問題