简体   繁体   English

尝试在 NumPy 中使用 NetworkX 在 2d numpy 数组中查找邻居

[英]Trying to find neighbors in an 2d numpy array using NetworkX in NumPy

So i want to ask the user for the coordinates (ie (0,0)) and pass them on to find the neighbors.所以我想向用户询问坐标(即(0,0))并将它们传递给邻居。 How do I effectively access the tuple to have it passed on to get my result?我如何有效地访问元组以传递它以获得我的结果? It wont recognize it as it is a string and with the parenthesis.它不会识别它,因为它是一个字符串并带有括号。 Some Error Warning with the input specified after the dash:破折号后指定输入的一些错误警告:

networkx.exception.NetworkXError: The node ('(', '0') is not in the graph. - (0,0) networkx.exception.NetworkXError: 节点 ('(', '0') 不在图中。- (0,0)

networkx.exception.NetworkXError: The node ('0', '0') is not in the graph. networkx.exception.NetworkXError:节点('0','0')不在图中。 - 00 - 00

networkx.exception.NetworkXError: The node ('0', ',') is not in the graph. networkx.exception.NetworkXError:节点('0',',')不在图中。 - 0,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))

Why don't you sterilize your inputs so you only allow numbers and require two different inputs?你为什么不消毒你的输入,所以你只允许数字并需要两个不同的输入? Asking a human to put in whatever they want is asking for issues.要求一个人输入他们想要的任何东西都是在问问题。

In any case the trouble is that your format is a string and your needing decimal form.无论如何,问题在于您的格式是字符串并且您需要十进制形式。 Wrap your coords with int()用 int() 包裹你的坐标

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM