Python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Python

How to get position a number(for example 23) in matrix(list in list) python

28th Nov 2020, 8:25 PM
Mohammad
Mohammad - avatar
5 Answers
+ 4
Mohammad , do you mean a python list or a numpy array?
28th Nov 2020, 8:54 PM
Lothar
Lothar - avatar
+ 4
Mohammad , when you can present us your attempt, we can help you with a solution. Until then, I give you some hints: - use a nested for loop for the outer and the inner list - in the outer loop, also use an enumerator to get its index position - in the inner loop. check if the value you are looking for is a member of the inner list - if yes, get its index by using the index() method, print the positions you have as [ndx1][ndx2]
29th Nov 2020, 1:03 PM
Lothar
Lothar - avatar
+ 1
Lothar For example i want to get position of 23 in this matrix: [[14,17,11],[10,23,42],[20,13,71]]
28th Nov 2020, 8:57 PM
Mohammad
Mohammad - avatar
+ 1
Mohammad 23 in your list will be: lst[1][1] Nested list example showing index's: [[0,1,2],[0,1,2],[0,1,2]]
28th Nov 2020, 9:57 PM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
you can use numpy.where like this: arr = np.array([[14,17,11],[10,23,42],[20,13,71]]) print(*zip(*np.where(arr == 23))) # (1, 1)
29th Nov 2020, 3:28 AM
o.gak
o.gak - avatar