[Solved] Anybody knows how to do a binary search with a 2d array of strings? [closed]


Basically I want to look at the position in where all my “X” are.

You can use np.where to get the indices for each axis, then zip them to get index tuples for all the locations:

>>> list(zip(*np.where(Arr == "X")))
[(3, 2), (5, 4), (6, 1)]

If you want the (row, column) “locations”, you could do this:

>>> [(Arr[row, 0], Arr[0, col]) for row, col in zip(*np.where(Arr == "X"))]
[('3', 'B'), ('5', 'D'), ('6', 'A')]

However, you seem to be treating an array as a table. You should consider using Pandas:

>>> df = pd.DataFrame(Arr[1:, 1:], columns=Arr[0, 1:], index=range(1, len(Arr[1:]) + 1))

>>> df
   A  B  C  D  E  F
1  0  0  0  0  0  0
2  0  0  0  0  0  0
3  0  X  0  0  0  0
4  0  0  0  0  0  0
5  0  0  0  X  0  0
6  X  0  0  0  0  0
7  0  0  0  0  0  0
8  0  0  0  0  0  0

>>> rows, cols = np.where(df == "X")

>>> [*zip(df.index[rows], df.columns[cols])]
[(3, 'B'), (5, 'D'), (6, 'A')]

2

solved Anybody knows how to do a binary search with a 2d array of strings? [closed]