Ann*_*hen 6 python recursion list python-2.7
问题发生在我使用图论的实验的继续,所以作为输入我还将添加我的图的图片:
但我也在几个数组中描述了这个图:首先,所有连接的数组:
arrAllConnections = [['F', 'G'], ['G', 'F'], ['G', 'N'], ['N', 'G'], ['N', 'E'], ['E', 'N'], ['E', 'D'], ['D', 'E'], ['D', 'C'], ['C', 'D'], ['C', 'B'], ['B', 'C'], ['B', 'A'], ['A', 'B'], ['A', 'E'], ['E', 'A'], ['B', 'X8'], ['X8', 'B'], ['X8', 'X3'], ['X3', 'X8'], ['X3', 'X2'], ['X2', 'X3'], ['C', 'T'], ['T', 'C'], ['T', 'T1'], ['T1', 'T'], ['T1', 'Y'], ['Y', 'T1'], ['Y', 'L'], ['L', 'Y'],['L', 'P'], ['P', 'L'], ['P', 'Z'], ['Z', 'P'], ['Z', 'Y'], ['Y', 'Z'], ['L', 'K3'], ['K3', 'L'], ['Z', 'K1'], ['K1', 'Z'], ['K1', 'K2'], ['K2', 'K1']]
Run Code Online (Sandbox Code Playgroud)
我认为它包含一些额外的信息并添加了这样一行:
arrAllConnections = [list(i) for i in set(map(tuple, arrAllConnections))]
Run Code Online (Sandbox Code Playgroud)
还有一些节点我想连接到其他节点组.节点组是循环,这是循环列表:
arrWithWhatToConnect = [['A', 'B', 'C', 'D', 'E'], ['Z', 'P', 'L', 'Y']]
Run Code Online (Sandbox Code Playgroud)
要连接的节点大多数是循环外的节点,但它们可能在"内部"(例如:'E'节点):
arrWhatToConnect = ['F', 'G', 'N', 'X2', 'X3', 'K3', 'K1', 'K2', 'E']
Run Code Online (Sandbox Code Playgroud)
如果您查看图像,您可能会注意到以下几点:
X2连接到X3,连接到X8,但由于后者不在列表中,arrWhatToConnect所以必须停止该过程;E节点已经在循环中,因此一旦E在循环中找到该过程就会停止;F需要递归,简而言之,它应该是这样的:if F not in cycle/arrWithWhatToConnect, but in arrWhatToConnect -> check next node and repeat check for cycle/arrWithWhatToConnect or/|| arrWhatToConnect.最后,这是我想要得到的:
for 'E' node -> ['A', 'B', 'C', 'D', 'E']
for 'F' node -> ['F', 'G', 'N', 'A', 'B', 'C', 'D', 'E']
etc
Run Code Online (Sandbox Code Playgroud)
这些列表包含所选节点和周期之间的最短路径.
我的代码:
arrWhatToConnect = ['F', 'G', 'N', 'X2', 'X3', 'K3', 'K1', 'K2', 'E']
#THERE IS NO X8!!!!!
arrWithWhatToConnect = [['A', 'B', 'C', 'D', 'E'], ['Z', 'P', 'L', 'Y']]
arrAllConnections = [['F', 'G'], ['G', 'F'], ['G', 'N'], ['N', 'G'], ['N', 'E'], ['E', 'N'], ['E', 'D'], ['D', 'E'], ['D', 'C'], ['C', 'D'], ['C', 'B'], ['B', 'C'], ['B', 'A'], ['A', 'B'], ['A', 'E'], ['E', 'A'], ['B', 'X8'], ['X8', 'B'], ['X8', 'X3'], ['X3', 'X8'], ['X3', 'X2'], ['X2', 'X3'], ['C', 'T'], ['T', 'C'], ['T', 'T1'], ['T1', 'T'], ['T1', 'Y'], ['Y', 'T1'], ['Y', 'L'], ['L', 'Y'],['L', 'P'], ['P', 'L'], ['P', 'Z'], ['Z', 'P'], ['Z', 'Y'], ['Y', 'Z'], ['L', 'K3'], ['K3', 'L'], ['Z', 'K1'], ['K1', 'Z'], ['K1', 'K2'], ['K2', 'K1']]
nullFirstConnect = []
#arrAllConnections = (sorted(item for item in arrAllConnections))
arrAllConnections = [list(i) for i in set(map(tuple, arrAllConnections))]
print(arrAllConnections, 'REMOVED?')
def connect(arrWithWhatToConnect, arrWhatToConnect, arrAllConnections, item, olderItems):
arrAllConDel = arrAllConnections
print('CONTENTS OF TRUNCATED ARR CONNECTIONS', arrAllConDel)
arrConnected = []
for cycle in arrWithWhatToConnect:
if item in cycle:
arrConnected.extend(cycle)
arrConnected.extend(olderItems)
print('My item in cycle', item, cycle, arrConnected)
else:
print('Not in cycle', item)
print('GOING TO CHECK ELSEWHERE!')
olderItems.extend(item)
olderItems.extend(olderItems)
for connection in arrAllConnections:
item1 = connection[0]
item2 = connection[1]
if item in connection:
if item == item1:
print('connection is', connection, 'item', item, 'item1', item1, 'item2', item2)
if item2 in arrWhatToConnect:
del arrAllConDel[arrAllConnections.index(connection)]
connect(arrWithWhatToConnect, arrWhatToConnect, arrAllConDel, item2, olderItems)
elif item == item2:
if item1 in arrWhatToConnect:
del arrAllConDel[arrAllConnections.index(connection)]
connect(arrWithWhatToConnect, arrWhatToConnect, arrAllConDel, item1, olderItems)
return arrConnected
for item in arrWhatToConnect:
print(item)
print(connect(arrWithWhatToConnect, arrWhatToConnect, arrAllConnections, item, nullFirstConnect))
Run Code Online (Sandbox Code Playgroud)
这是我第一次尝试编写任何递归(或者如果我不记得它是一个已经实现的写递归的尝试;)).一切似乎都或多或少都清晰,甚至有效.但是没有足够的返回语句,这会对内存产生影响.以及我似乎应该添加visitedNodes数组.如果您可以告诉我如何改进我的代码,请执行此操作.谢谢.最好在python中没有任何模块,比如nx等,我想了解里面发生了什么.