我在尝试编写递归函数时遇到了问题。看起来函数没有下降。我没有收到任何错误或任何东西,所以我真的不明白发生了什么。我从中得到的唯一输出是一些空列表。从本质上说,我试图跟踪我的程序可以通过某些给定输入的所有路径,并返回一个包含所有这些路径的列表。我对python真的,真的,真的很陌生,所以有很多我不知道。我对stackoverflow也很陌生,所以请原谅格式错误。提前谢谢!你知道吗
def Process ( fst, input, start_state, current_path=[], input_index=0 ):
current_line = input.replace('"', '')
current_state = start_state
probability = 1
result = []
state_paths = []
this_path = current_path
paths_found = []
epsilon_paths_found = []
temp_list = []
index = input_index
if not index < len( current_line ):
return this_path
item = current_line[index]
if not item.isspace():
for edge in fst.transitions[current_state]:
next_state = current_state
if item == edge.input:
paths_found.append( edge )
index += 1
for x in paths_found:
temp_list = this_path
temp_list.append( x )
temp_entry = Process( fst, input, x.target_state, temp_list, index )
state_paths.append( temp_entry )
#epsilon returns a list
epsilon = EpsilonStates( fst.transitions[current_state] )
if epsilon:
index -= 1
for i in epsilon:
epsilon_paths_found.append( i )
for y in epsilon_paths_found:
temp_list = this_path
temp_list.append( y )
state_paths.append( Process( fst, input, y.target_state, temp_list, index ) )
return state_paths
为了实现递归,您的进程函数需要调用自身。通常使用初始函数参数的子集。你知道吗
在代码中,您是从for循环调用Process函数。所以这不是递归的。你知道吗
这个线程有一个递归函数的例子,以及为什么在后续调用中减少参数的例子:
How can I build a recursive function in python?
相关问题 更多 >
编程相关推荐