2012-11-13 2 views
6

EDIT:推奨回答とそれがまだ間に合いません。ツリー内のすべてのパスを上から下に検索します。

これにはStack Overflowに関する多くの類似の質問がありますが、Pythonではまったく同じものはありません。私はプログラミングの初心者ですので、簡単に行ってください。

このように私は、ネストされた辞書の木を持っている:

[{'word': 'The', 
    'next': [{'word': 'End', 
      'next': None}, 
      {'word': 'quick', 
      'next': [{'word': 'brown', 
         'next': [{'word': 'fox', 
           'next': None}]}]}, 
      {'word': 'best', 
      'next': [{'word': 'of', 
         'next': [{'word': 'times', 
           'next': None}]}]}]}] 

私は上から下へのすべてのパスを平らにし、これで終わるしたい:

[[{'word': 'The'}, 
    {'word': 'End'}], 

[{'word': 'The'}, 
    {'word': 'quick'}, 
    {'word': 'brown'}, 
    {'word': 'fox'}], 

[{'word': 'The'}, 
    {'word': 'best'}, 
    {'word': 'of'}, 
    {'word': 'times'}]] 

私は美しいを作りました最初の構造を作成した再帰関数はほとんどありませんでしたが、私はそれをunrecursivizingするのに苦労しています。私が得たので、これは限りです。これを返す

def flatten_combinations(result_tree, current_combo = None, all_combos = None): 
    if current_combo is None: 
     current_combo = [] 
    if all_combos is None: 
     all_combos = [] 
    if result_tree is None: 
     all_combos.append(current_combo) 
     return 
    for word in result_tree: 
     current_combo.append({'word': word['word']}) 
     flatten_combinations(word['next'], current_combo, all_combos) 
    return current_combo 

...:明確にやや近いが、非常に適切ではない

[{'word': 'The'}, 
{'word': 'End'}, 
{'word': 'quick'}, 
{'word': 'brown'}, 
{'word': 'fox'}, 
{'word': 'best'}, 
{'word': 'of'}, 
{'word': 'times'}] 

...。

私はその機能がおそらく恐らくPythonではないことを知っていますが、私はプログラミングを教えているので、このようなことから考え抜いてしまう可能性のある言語機能を利用しようとしていませんスクラッチ("彼は言った、Qの&のメンバーは、そのメンバーが彼が少しの考えを溶かすのを助けることを望むサイト)に投稿した。

So:どうしたのですか?

EDITは:これは近いまだですが、かなり右ではない

def flatten_combinations(result_tree, current_combo = None, all_combos = None): 
    if current_combo is None: 
     current_combo = [] 
    if all_combos is None: 
     all_combos = [] 
    if result_tree is None: 
     all_combos.append(current_combo) 
     return 
    for word in result_tree: 
     current_combo = current_combo[:] 
     current_combo.append({'word': word['word']}) 
     flatten_combinations(word['next'], current_combo, all_combos) 
    return all_combos 

[{'word': 'The'}, 
{'word': 'End'}], 

[{'word': 'The'}, 
{'word': 'End'}, 
{'word': 'quick'}, 
{'word': 'brown'}, 
{'word': 'fox'}], 

[{'word': 'The'}, 
{'word': 'End'}, 
{'word': 'quick'}, 
{'word': 'best'}, 
{'word': 'of'}, 
{'word': 'times'}]] 
+0

TIL "Elideのは" 言葉です。 – AndyPerfect

答えて

1

は、その内の2つのマイナーなミスがあります。それはあなたに最後の結果を与えるだけです

2)各の反復で、current_comboを変更します。まずコピーを作成してください!

new_current_combo = current_combo[:] 
new_current_combo.append({'word': word['word']}) 
flatten_combinations(word['next'], new_current_combo, all_combos) 

全コード:

def flatten_combinations(result_tree, current_combo=None, all_combos=None): 
    if current_combo is None: 
     current_combo = [] 
    if all_combos is None: 
     all_combos = [] 
    if result_tree is None: 
     all_combos.append(current_combo) 
     return 
    for word in result_tree: 
     new_current_combo = current_combo[:] 
     new_current_combo.append({'word': word['word']}) 
     flatten_combinations(word['next'], new_current_combo, all_combos) 
    return all_combos 
+0

閉じるがシガーはない。私は上記のこの関数の結果について少しは追加しました。 (私はまだそれ以上自分でそれをさらに修正しようとしていない。) –

+0

申し訳ありませんが、私は私の結果を誤解していた。一定! – Moshe

1

あなたがcurrent_comboのコピーを渡すとモシェは、下記の問題のいくつかを修正しました再帰呼び出しでは、forループの次の繰り返しで現在のパスを失うことはありません。

また、結果が再帰で使用されないため、current_comboを返す必要はありません。代わりにall_combosを戻し、それをパラメーターとして使用したくない場合があります。代わりに、再帰関数となし再帰関数を持つことができます。非再帰関数はall_combosのリストを作成し、それを再帰関数に渡して、再帰関数がall_combosが設定されていると仮定できるようにします。

1

私はこの戦略を取ると思います:各ツリーについて、

  1. 再帰をこのツリーのルートにある単語の後に来る文章のリストを計算します。
  2. 各文章の前に、現在の単語の前に追加します。
  3. 新しく拡張された文のリストを返します。

帰納法による証明をしましたか?私はプログラミングで最も役に立つ数学のテクニックの1つであることがわかります:

  1. あなたの関数が正しく「次」がNoneであるツリーを処理することを証明してください。
  2. 関数が、深さがn-1のツリーを正しく処理できると仮定して、関数が深さがnのツリーを処理することを証明します。

誘導は、任意の深さの樹木をカバーするために証明を拡張します。完了!

1)あなたはcurrent_combo代わりの戻る:

0

だけJoshHeitzmanの答えコンクリート@作る(およびデフォルトののparamsを簡素化):

def flatten_combinations(result_tree, current_combo = [], all_combos = []): 
if result_tree is None: 
    all_combos.append(current_combo) 
    return 
for word in result_tree: 
    current_combo.append({'word': word['word']}) 
    flatten_combinations(word['next'], current_combo[:], all_combos) 
return all_combos 
+0

リストをデフォルトの引数として渡すのが悪いです! [Python docs](http://docs.python.org/2/tutorial/controlflow.html#default-argument-values)を参照してください。 – Moshe