Python 在reccursion调用期间追加列表项(最后我得到一个空列表)
我有一个列表列表(总共10个元素),我正在尝试计算图中强连接组件的数量(但不用担心) 下面是我的列表的样子:Python 在reccursion调用期间追加列表项(最后我得到一个空列表),python,list,recursion,Python,List,Recursion,我有一个列表列表(总共10个元素),我正在尝试计算图中强连接组件的数量(但不用担心) 下面是我的列表的样子: df_reversed_back =[[9, 7], [8, 9], [7, 8], [6, 9], [6, 4], [5, 6], [4, 5], [3, 5], [3, 1], [2, 3], [1, 2]] 其中,内部列表的第一个元素-是顶点编号1[9],第二个元素是第二个顶点[7] 我的问题是,在递归调用中,我在列表中附加了一个列表,该列表存在于函数之外 a
df_reversed_back =[[9, 7],
[8, 9],
[7, 8],
[6, 9],
[6, 4],
[5, 6],
[4, 5],
[3, 5],
[3, 1],
[2, 3],
[1, 2]]
其中,内部列表的第一个元素-是顶点编号1[9],第二个元素是第二个顶点[7]
我的问题是,在递归调用中,我在列表中附加了一个列表,该列表存在于函数之外
all_components = []
SSC = []
explored= []
#c)modification of DFS
def DFS_2_Path(graph,node):
#global SSC
global all_components
explored.append(node)
print('Node:',node)
#index = [ind for ind,vertex in enumerate(df_reverse) if vertex[0] == node]
for second_vert in graph:
print('Second_vert:',second_vert)
print('Second_vert[0] == node:',second_vert[0] == node)
if second_vert[0] == node:
print('second_vert[1] not in explored :',second_vert[1] not in explored)
if second_vert[1] not in explored:
print('SSC was:',SSC)
SSC.append(second_vert[1])
print('SSC is:',SSC)
print('---------------------------------')
print('NEXT ITERATION OF THE INNER LOOP')
print('-------------------------------------')
DFS_2_Path(graph,second_vert[1])
if second_vert[1] in explored and len(SSC)> 0 :#check if second vert is not explored and if it's not a new SSC
print('SSC was:',SSC)
SSC.append(second_vert[1])
print('SSC is:',SSC)
all_components.append(SSC)
print('All_components is :',all_components)
SSC[:] = []
print('All_components was:',all_components)
for i in range(max(df_reversed_back[0]),0,-1):
if i not in explored:
s = i
DFS_2_Path(df_reversed_back,i)
如您所见,我想将SSC附加到所有_组件。
结果必须是:所有_分量=[[7,8,9],[4,5,6],[1,2,3]]
但最后我得到了:所有的组件=[],[],[]
你能告诉我哪里出错了吗?你的问题在这里:
all_components.append(SSC)
print('All_components is :',all_components)
SSC[:] = []
您将SSC
作为所有组件的一个元素,但立即将其清空。这会将所有组件保留为一个包含空列表的列表。然后继续使用主列表(SSC)进行操作。您可以在输出中看到这一点:
SSC was: [1, 2]
SSC is: [1, 2, 3]
All_components is : [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
Second_vert: [1, 2]
Second_vert[0] == node: False
All_components was: [[], [], []]
All_components was: [[], [], []]
每个元素只是对原始SSC
的引用:您将其视为三元组,即空的或最终的组件
我怀疑您希望附加SSC
的快照(副本):
all_components.append(SSC[:])
print('All_components is :',all_components)
SSC[:] = []
输出:
...
All_components was: [[7, 8, 9], [4, 5, 6], [1, 2, 3]]
你的问题是:
all_components.append(SSC)
print('All_components is :',all_components)
SSC[:] = []
您将SSC
作为所有组件的一个元素,但立即将其清空。这会将所有组件保留为一个包含空列表的列表。然后继续使用主列表(SSC)进行操作。您可以在输出中看到这一点:
SSC was: [1, 2]
SSC is: [1, 2, 3]
All_components is : [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
Second_vert: [1, 2]
Second_vert[0] == node: False
All_components was: [[], [], []]
All_components was: [[], [], []]
每个元素只是对原始SSC
的引用:您将其视为三元组,即空的或最终的组件
我怀疑您希望附加SSC
的快照(副本):
all_components.append(SSC[:])
print('All_components is :',all_components)
SSC[:] = []
输出:
...
All_components was: [[7, 8, 9], [4, 5, 6], [1, 2, 3]]
执行此操作时,您正在销毁SSC的内容:
all_components.append(SSC)
print('All_components is :',all_components)
SSC[:] = []
这将有效地将对SSC
的引用添加到所有组件
,然后删除全局变量所指向的数组的内容
也许更好的方法是在需要时创建一个新的SSC,将其作为函数的参数
您可以将函数设置为:
def DFS_2_Path(graph,node, SSC=None):
if SSC == None:
SSC = []
然后,当您递归调用它时,使用以下命令将其传递到递归中:
DFS_2_Path(graph,second_vert[1], SSC)
这将允许每个递归路径获得它自己的SSC
数组,作为奖励,您不需要全局变量。执行此操作时,您正在销毁SSC
的内容:
all_components.append(SSC)
print('All_components is :',all_components)
SSC[:] = []
这将有效地将对SSC
的引用添加到所有组件
,然后删除全局变量所指向的数组的内容
也许更好的方法是在需要时创建一个新的SSC,将其作为函数的参数
您可以将函数设置为:
def DFS_2_Path(graph,node, SSC=None):
if SSC == None:
SSC = []
然后,当您递归调用它时,使用以下命令将其传递到递归中:
DFS_2_Path(graph,second_vert[1], SSC)
这将允许每个递归路径获得它自己的SSC
数组,作为奖励,您不需要全局变量。我不明白为什么首先要递归地执行此操作,因为您在此处清除了SCC
的内容:SSC[:]=[]
我不明白为什么首先要递归地执行此操作,因为您在此处清除了SCC
的内容:SSC[:]=[]