Reputation: 35
What I am trying to do is take a user input for as many rows as they wish, and make it into an array. After that i want to find where the highest number is in the array, an (x,y) coordinate of the number type of thing. I have created the initial array in different way but for example I have:
import numpy as np
m = []
rows = eval(input("How many rows in the list:"))
for row in range(rows):
m.append([])
value = eval(input("Enter a row:"))
m[row].append(value)
m = np.array(m)
print(m)
M = np.amax(m)
print(M)
index = np.where(M)
print(index)
The printing is just so i can see what it is doing. As a final i just want to see the last line in this example:
Enter the number of rows in the list:3
Enter a row: 1,5,6
Enter a row: 2,6,7
Enter a row: 5,26,12
The location of the largest element is at (1, 2)
Extra info: I don't care if it uses numpy of not. and i wouldn't be against using separate functions, when i think about it it might be a good idea; one to create an array and a separate to locate the largest number.
Thanks in advance.
PS. As the comment says, eval shouldn't usually be used because of security risks. The only reason I am using it is because this is a quick assignment for school.
Upvotes: 1
Views: 7261
Reputation: 4000
This is an implementation without using numpy. It is not that efficient, but works fine.
rows = eval(input("How many rows in the list:"))
m = []
for row in range(rows):
value = eval(input("Enter a row:"))
m.append(value)
large = m[0][0]
x = 0
y = 0
for i in range(0, rows):
for j in range(0, len(m[i])):
if(large < m[i][j]):
large = m[i][j]
y = i
x = j
print(x, y, large)
Upvotes: 1
Reputation: 9081
This gives the (row,column) index of the max -
import numpy as np
m = np.array([[1,5,6],[2,6,7],[5,26,12]]) # input minified
print(m)
print np.unravel_index(m.argmax(), m.shape) # main code you need
Upvotes: 1