簡體   English   中英

如何使用 Numpy 數組查找給定值的 x 和 y 坐標

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

要使用 2D numpy 數組制作棋盤游戲,用戶輸入一個數字,我需要在單獨的變量中返回該數字的 X 和 Y 值。

這就是我設置數組的方式

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM