1

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.

1
  • 3
    Please don't use eval to convert arbitrary user input. eval and exec should generally be avoided because they can be a security risk. For details, please see Eval really is dangerous by SO veteran Ned Batchelder. Commented Nov 28, 2016 at 6:43

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

That works in a way. I am thinking it has something to do with my input and how the list is but its printing out a 3 dimensional coordinate. EX: its printing (2, 0, 1). I don't know why the zero is there and i don't want it there.
@zmorris that the way the input is being fed I think that might be the reason for the 3d thing...sorry I am using python 2.7 so eval(input()) was giving me errors...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.