zmorris zmorris - 1 year ago 105
Python Question

Find Element In Two-Dimensional Python Array

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):
value = eval(input("Enter a row:"))
m = np.array(m)
M = np.amax(m)
index = np.where(M)

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.

Answer Source

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:"))

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download