我想要做的就是根据用户输入想要的行数,并将其放入数组中。之后,我想找到数组中最高的数字,数字类型的事物(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)。 –