1
再帰とまったく同じ機能を実装しました。Pythonに再帰制限があり、データを共有する際に問題があるため再帰のないバージョンが必要です。リスト内の項目を別のリストの項目に再帰的に置換する
sublist2 = [{'nothing': "Notice This"}]
sublist1 = [{'include':[sublist2]}]
mainlist = [{'nothing': 1}, {'include':[sublist1, sublist2]},
{'nothing': 2}, {'include':[sublist2]}]
Todoには何が記入されますか?操作後
for i in mainlist:
if 'nothing' in i:
# Do nothing
else if 'include' in i:
# Todo
# Append the contents of the list mentioned recursively
# in it's own place without disrupting the flow
期待される結果
mainlist = [{'nothing': 1},
{'nothing': "Notice This"}, {'nothing': "Notice This"},
{'nothing':2},
{'nothing': "Notice This"}]
あなたはsublist2にsublist1参照が気付いた場合。 {: "お知らせこの" '何もない'}、{ '何': "お知らせこの"}
によって置き換えられます。それは
{[sublist1、sublist2は] 'を含む'}理由です私は、次の
代わりに再帰を使用しての、あなただけのn番目の要素を見て、それを変更Inserting values into specific locations in a list in Python
How to get item's position in a list?
に割り当てることの違いを私は混乱しています。あなたはこれを再帰的にやりたいのですか?あなたの最初のパラグラフとタイトルは互いに矛盾します。 – Jokab
Pythonに再帰制限(デフォルトは1000)があるのは間違いありませんが、データ構造がそれを破っている場合は、データに深刻な問題があると言います。 –
@ Jokab私はリストが再帰的に設定されるべきであることを意味しました。私は再帰を使用したくありません。 –