2D列表中查找索引我需要找到索引中的2D陣列,其中的數量是任一比編號,以左,右和小於上面和下面,或反之亦然的數字越大。在用條件
我知道我需要兩個for循環一個用於行第一列,我給了數字x,我需要將它與2d數組中的數字進行比較以查找索引。
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]]
如果我給4爲x,我需要找到索引在2D列表,向左,向右,向上和向下,反之亦然其他數大於或小於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語句來檢查大於小於的問題。
不是家庭作業服務,通過嘗試解決方案讓我們半途而廢。 – ospahiu
嘗試加入的問題 – mcfellows