[英]Python, Numpy. Find values in 2d array and replace neighbors with 1
[英]Trying to find neighbors in an 2d numpy array using NetworkX in NumPy
所以我想向用戶詢問坐標(即(0,0))並將它們傳遞給鄰居。 我如何有效地訪問元組以傳遞它以獲得我的結果? 它不會識別它,因為它是一個字符串並帶有括號。 破折號后指定輸入的一些錯誤警告:
networkx.exception.NetworkXError: 節點 ('(', '0') 不在圖中。- (0,0)
networkx.exception.NetworkXError:節點('0','0')不在圖中。 - 00
networkx.exception.NetworkXError:節點('0',',')不在圖中。 - 0,0
def get_neighbors(self, coords):
return list(nx.grid_2d_graph(*self.get_face_value().shape).neighbors((coords[0], coords[1])))
def __repr__(self):
x = tuple(input('What coordinates in the array shown above would you like to use? ').strip(','))
return 'Adjacent values at the coordinates specified of side ' + str(self.get_side_chosen()) + ' are ' + \
str(self.get_neighbors(x))
你為什么不消毒你的輸入,所以你只允許數字並需要兩個不同的輸入? 要求一個人輸入他們想要的任何東西都是在問問題。
無論如何,問題在於您的格式是字符串並且您需要十進制形式。 用 int() 包裹你的坐標
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.