0

書き込もうとしている再帰関数に問題があります。関数が下降していないようです。エラーも何も出ていないので、何が起こっているのかよくわかりません。そこから得られる唯一の出力は、いくつかの空のリストです。基本的に、プログラムが特定の入力を介して通過できるすべてのパスを追跡し、それらすべてのパスを保持するリストを返そうとしています。私は本当に、本当に、本当にPythonに慣れていないので、知らないことがたくさんあります。私はスタックオーバーフローも初めてなので、フォーマットエラーを許してください。前もって感謝します!

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
4

1 に答える 1

-2

再帰が発生するには、プロセス関数がそれ自体を呼び出す必要があります。通常、初期関数引数のサブセットを使用します。

コードでは、for ループから Process 関数を呼び出しています。したがって、これは再帰的ではありません。

このスレッドには、再帰関数の例と、後続の呼び出しで引数が削減される理由の例があります。

Pythonで再帰関数を作成するにはどうすればよいですか?

于 2013-10-17T23:39:10.653 に答える