以下是一些示例词典:
G = {0:[1,2], 1:[0], 2:[0]}
V = {0:[0,2], 1:[0], 2:[0]}
W = {0:[1,2], 1:[4], 2:[0]}
X = {0:[2], 1:[0], 2:[0]}
Y = [2,3,4]
为了检查它们是否表示一个图,我试图检查没有节点将自身作为邻居,列为邻居的唯一事物是有效的节点,如果a将b作为邻居,那么b也将a作为邻居,并且所有对象都具有适当的类型
def IsItAGraph(D):
if type(D) is dict:
for x in D.keys():
if x not in D[x]:
for y in D[x]:
if y in D.keys():
if y in D[x]:
if x in D[y]:
return True
else:
return False
else:
return False
else:
return False
else:
return False
N = [IsItAGraph(Y), IsItAGraph(G), IsItAGraph(V), IsItAGraph(W), IsItAGraph(X)]
当我把它作为输入发送时,作为输出,我得到字典的True
和所有其他的X
和False
,除了G
之外,我应该得到所有的False
我做错了什么
您可以编写一个函数来检查每个需求
那么比如说
另外,我只是想澄清一下,我基本上遵循了你问题中的要求,但这些不一定是所有图形的要求。比如说
相关问题 更多 >
编程相关推荐