私は4レベルのネストされた辞書構造で作業しています。ネストされた辞書全体を繰り返し処理し、個々の辞書に識別番号を付けようとしています(アイテムのツリーを構築し、どのアイテムノードかを知るための前兆として)親であり、ノードが持つ子など)
私はこの機能を持っています:
def r(y):
    cnt = 1
    def recurse(y, count):
        for i in y.iteritems():
            count+=1
            i['id'] = count
            for k,v in y.iteritems():
                if isinstance(v, list):
                    [recurse(i, count) for i in v]
                else:
                    pass
    recurse(y, cnt)
    return y
辞書のリストのネストされた辞書に入れました。
つまり、思ったように機能しません。
{'sections': [{'id': 11, 'info': 'This is section ONE', 'tag': 's1'},
              {'fields': [{'id': 15,
                           'info': 'This is field ONE',
                           'tag': 'f1'},
                          {'elements': [{'id': 20,
                                         'info': 'This is element',
                                         'tag': 'e1',
                                         'type_of': 'text_field'},
                                        {'id': 20,
                                         'info': 'This is element',
                                         'tag': 'e2',
                                         'type_of': 'text_field'},
                                        {'id': 20,
                                         'info': 'This is element',
                                         'tag': 'e3',
                                         'type_of': 'text_field'},
                                        {'id': 20,
                                         'info': 'This is element',
                                         'tag': 'e4',
                                         'type_of': 'text_field'}],
                           'id': 16,
                           'info': 'This is field TWO',
                           'tag': 'f2'},
                          {'elements': [{'id': 20,
                                         'info': 'This is element',
                                         'tag': 'e5',
                                         'type_of': 'text_field'},
                                        {'id': 20,
                                         'info': 'This is element',
                                         'tag': 'e6',
                                         'type_of': 'text_field'},
                                        {'id': 20,
                                         'info': 'This is element',
                                         'tag': 'e7',
                                         'type_of': 'text_field'},
                                        {'id': 20,
                                         'info': 'This is element ONE',
                                         'tag': 'e8',
                                         'type_of': 'text_field'}],
                           'id': 16,
                           'info': 'This is field THREE',
                           'tag': 'f3'}],
               'id': 12,
               'info': 'This is section TWO',
               'tag': 's2'},
              {'fields': [{'id': 15,
                           'info': 'This is field FOUR',
                           'tag': 'f4'},
                          {'id': 15,
                           'info': 'This is field FIVE',
                           'tag': 'f5'},
                          {'id': 15,
                           'info': 'This is field SIX',
                           'tag': 'f6'}],
               'id': 12,
               'info': 'This is section THREE',
               'tag': 's3'}],
 'tag': 'test'}
私がやりたいことは、レベル 1 のすべてのアイテムに番号が付けられ、次にレベル 2 のすべてのアイテムに番号が付けられ、次にレベル 3、次にレベル 4 に番号が付けられることです。この場合、メイン アイテムに 1 の ID を指定し、次にセクションを 2、3、4 として識別し、次にフィールドを 5 として識別し、次に要素などを識別します。開始しますが、かなり間違っています。
編集:私が本当に必要なのは、ネストされた辞書構造から親/子ノードのツリーを作成して、必要に応じてこのツリーのアイテムを反復/挿入/取得/操作できるようにすることです。それを行う簡単な方法はありますか?思った以上に頑張っているようです。
EDIT2:元の質問に対する解決策を見つけました。ID を追加する余分な手順の代わりに、組み込みの id() 関数を使用することにしたところ、必要な最小限のツリーを作成できましたが、これはまだ演習として役立ちます。