我想要做的就是根據用戶輸入想要的行數,並將其放入數組中。之後,我想找到數組中最高的數字,數字類型的事物(x,y)座標。我以不同的方式創建了初始數組,但例如我有:在二維Python數組中尋找元素
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)
打印只是讓我可以看到它在做什麼。作爲最後我只是想看看最後一行在這個例子:
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)
額外的信息:我不在乎,如果它使用的不是numpy的。我不會反對使用單獨的功能,當我想到這可能是一個好主意;一個創建一個數組並單獨定位最大的數字。
在此先感謝。
PS。正如評論所說,由於安全風險,通常不應該使用eval。我使用它的唯一原因是因爲這是學校的快速任務。
請不要使用'eval'來轉換任意的用戶輸入。一般應避免使用「eval」和「exec」,因爲它們可能存在安全風險。有關詳情,請參閱SO老將Ned Batchelder的[Eval really dangerous](http://edbatchelder.com/blog/201206/eval_really_is_dangerous.html)。 –