+ 1

Returning the indices of a value in a 2D array

I'm trying to create a system that returns the indices of a value in a 2D array, with values from A1 to J10. I can create the array: import numpy as np rows = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"] col = list(range(1, 11)) columns = [] for i in col: columns.append(str(i)) grid = [i + j for i in rows for j in columns] #creates a single list with every cell org = np.array(grid) #makes an array out of the list object 'grid' array = np.array_split(org, 10) #splits the array into 10 rows And I have no problem getting the value of a known index from the array ("D4" in this example) print(array[3][3]) But I can't do it the other way around, i.e. get the index of a known value. I've tried using index and where, and neither of them behave as I would expect: print(np.where(array == "D4")) print(array.index("D4")) What do I need to change to make either of them work?

3rd Jan 2023, 5:19 PM
Savager
2 Respostas
+ 2
An array of arrays, is not exactly the same as a multidimensional array (in numpy). I suggest to get acquainted with the reshape() function. Then slicing will be a little different (you can use two indices in one bracket, rather than two brackets). But the where function works like a charm. See sample code, also I am showing an easier way to create your initial data with a single list comprehension. https://code.sololearn.com/c6tgg1QygeeQ/?ref=app Great question! :)
3rd Jan 2023, 6:18 PM
Tibor Santa
Tibor Santa - avatar
+ 1
Thank you very much for your help!
3rd Jan 2023, 9:23 PM
Savager