Python 如何使用Numpy数组查找给定值的x和y坐标
要使用2D numpy数组制作棋盘游戏,用户需要输入一个数字,我需要在单独的变量中返回该数字的X&Y值 这就是我设置阵列的方式Python 如何使用Numpy数组查找给定值的x和y坐标,python,arrays,numpy,coordinates,Python,Arrays,Numpy,Coordinates,要使用2D numpy数组制作棋盘游戏,用户需要输入一个数字,我需要在单独的变量中返回该数字的X&Y值 这就是我设置阵列的方式 board = np.array(["01","02","03","04","05","06","07","08", "09","10&q
board = np.array(["01","02","03","04","05","06","07","08",
"09","10","11","12","13","14","15","16",
"17","18","19","20","21","22","23","24",
"25","26"," ","@@","29","30","31","32",
"33","34","35","@@"," ","38","39","40",
"41","42","43","44","45","46","47","48",
"49","50","51","52","53","54","55","56",
"57","58","59","60","61","62","63","64"])
board = board.reshape(8,8)
这可以通过多种方式实现。其中之一可以是:
value = input("Enter the board value") # This allows the user to enter the value
found = np.squeeze(np.where(value == board)) # This numpy function finds where the array is equal to the board value
if found.size > 0: # checks if the found value is not empty
print('The value is present in the board at {}, {}'.format(found[0], found[1]))
else:
print('Enter a valid input')
谢谢,明白了working@WilliamKavanagh很高兴知道。然而,这将导致一个不明确的结果,即两次出现这种情况。如果您希望将其作为特殊用途的case,可以将其添加为elseif(在python:elif中)case。你想让我来处理吗?或者你想把它作为一个学习过程,自己去弄清楚。我建议晚一点;)由于阵列的性质,我知道它不会抛出双阵列,但感谢您的提议。我把它作为一个学习的经验,因为我觉得我将要使用numpy alotin这段代码的东西将不得不去非常错误的双重发生,但感谢你的提议。我想我现在会把它当作一种学习经验。我不想依赖这个网站