简体   繁体   English

如何使用 Numpy 数组查找给定值的 x 和 y 坐标

[英]How to find the x and y coordinates of a given value using Numpy array

To make a board game using a 2D numpy array, user to input a number and I need X & Y value of that number being returned in separate variables.要使用 2D numpy 数组制作棋盘游戏,用户输入一个数字,我需要在单独的变量中返回该数字的 X 和 Y 值。

This is how I'd set up the array这就是我设置数组的方式

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)

It could be done in many ways.它可以通过多种方式完成。 One of it could be as follows:其中之一可能如下:

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在python数组中查找值的X和Y坐标 - Find X and Y Coordinates of a value in a python array 在 numpy 数组中查找具有指定 x 值的点的 y 坐标的最佳方法是什么 - What is the best way to find y coordinates of point with specified x value in numpy array 在numpy数组中使用y查找特定的x值 - Find specific x value with y in numpy array 查找给定z坐标值的相应[x,y]坐标 - Find the corresponding [x,y] coordinates for a given z coordinate value numpy 的 interp function - 如何找到给定 y 值的 x 值? - numpy's interp function - how to find a value of x for a given value of y? 在Numpy数组中如何查找值的所有坐标 - In Numpy array how to find all of the coordinates of a value 如何使用 x 和 y 坐标循环遍历 2D numpy 数组而不会出现越界错误? - How to loop through 2D numpy array using x and y coordinates without getting out of bounds error? 如何根据坐标x和y创建给定纬度的纬度数组? - How to create array of latitude given latitude as a function of coordinates x and y? 将 x、y 坐标表示为 numpy 数组 - representing x, y coordinates as numpy array 从坐标 [x,y] 的 numpy 数组中,删除具有相同 x 值的其他坐标以保留对 y 具有最大值的坐标 - From a numpy array of coordinates[x,y], remove other coordinates with a same x-value to keep the coordinate which has the maximum for y
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM