如何用递归替换嵌套的for循环?

4
首先,我对Python仍然是一个新手,请不要过于苛刻。
我已经做了一些研究,并基本理解了如何编写递归函数,但我完全无法理解面前的任务。
我真的希望能得到一些提示和指针,以便我可以自己完成并真正理解它。
以下是任务:
1. 迭代字典 2. 检查是否有“children”键 3. 如果是,则执行某些操作 4. 重复
问题在于,“option”键嵌套在另一个名为“children”的键中。因此,我想遍历每个“children”键,直到不存在为止。
以下是没有递归的代码。当然会失败,因为它太明确、过于死板。
#level(one/two/three) refer to the level of nesting, easier for me to know where I am     in the loop.
#qdf_v2 is a particular type of file which is essentially a dictionary 

for levelone in qdf_v2['children']:
    if 'children' in levelone:
        store = options
        for leveltwo in levelone['children']:
            if 'children' in leveltwo:
                store = options
                for levelthree in leveltwo['children']:
                    if 'children' in levellevel:
                        repeat....

我希望能够循环遍历字典及其“children”键,直到它们不再存在。
以下是一些JSON,您可以将其复制并粘贴到 http://www.jsoneditoronline.org/ 中以查看我正在使用的内容。
{
    "base": "http://panoptic-fearless.ldc.yougov.net/questionnaires/Test_maj_demo_newG4_2/versions/12/",
    "children": [
        {
            "name": "one",
            "children": [
                {
                    "name": "measures",
                    "children": [
                        {
                            "type": "single",
                            "options": {
                                "sort_order": "asending",
                                "chart_layout": "5",
                                "chart_type": "bar",
                                "chart_color": "green"
                            },
                            "text": "How much would you say you like or dislike P.E. at school? ",
                            "name": "KYS_Q1",
                            "responses": {
                                "items": [
                                    {
                                        "code": 1,
                                        "text": "I love it",
                                        "element": "qdf:response"
                                    },
                                    {
                                        "code": 2,
                                        "text": "I like it",
                                        "element": "qdf:response"
                                    },
                                    {
                                        "code": 3,
                                        "text": "I don’t like it",
                                        "element": "qdf:response"
                                    },
                                    {
                                        "code": 4,
                                        "text": "I hate it",
                                        "element": "qdf:response"
                                    },
                                    {
                                        "code": 5,
                                        "text": "Don’t know",
                                        "element": "qdf:response"
                                    }
                                ],
                                "element": "qdf:response_group"
                            },
                            "element": "qdf:question",
                            "export": [
                                {
                                    "name": "KYS_Q1"
                                }
                            ]
                        },
                        {
                            "children": [
                                {
                                    "type": "single",
                                    "options": {
                                        "sort_order": "asending",
                                        "chart_layout": "4",
                                        "chart_type": "pie",
                                        "chart_color": "pink"
                                    },
                                    "text": "{multiple order=\"randomize\" max=3} You said that you $event P.E. at school...<br/> Which THREE of the following are your MAIN reasons for not liking P.E. at school? (Please tick up to three options)",
                                    "name": "KYS_Q2",
                                    "responses": {
                                        "items": [
                                            {
                                                "code": 1,
                                                "text": "I don't like going outside in bad weather (e.g. in the cold, rain etc.)",
                                                "element": "qdf:response"
                                            },
                                            {
                                                "code": 2,
                                                "text": "I’m always picked last to join a team",
                                                "element": "qdf:response"
                                            },
                                            {
                                                "code": 3,
                                                "text": "I don't like my P.E. teacher/ don’t think they are very good",
                                                "element": "qdf:response"
                                            },
                                            {
                                                "code": 4,
                                                "text": "I’m not very good at it",
                                                "element": "qdf:response"
                                            },
                                            {
                                                "code": 5,
                                                "text": "I find the games/ sports we play boring",
                                                "element": "qdf:response"
                                            }
                                        ],
                                        "element": "qdf:response_group"
                                    },
                                    "element": "qdf:question",
                                    "export": [
                                        {
                                            "name": "KYS_Q2"
                                        }
                                    ]
                                },
                                {
                                    "type": "single",
                                    "options": {
                                        "sort_order": "asending",
                                        "chart_layout": "5",
                                        "chart_type": "line",
                                        "chart_color": "green"
                                    },
                                    "text": "{multiple order=\"randomize\"} Below are some feelings that people can have about doing P.E. at school...<br/> Which of the following words describe how you feel about doing P.E. at school? (Please tick all that apply) ",
                                    "name": "KYS_Q3A",
                                    "responses": {
                                        "items": [
                                            {
                                                "code": 1,
                                                "text": "Stressed",
                                                "element": "qdf:response"
                                            },
                                            {
                                                "code": 2,
                                                "text": "Scared",
                                                "element": "qdf:response"
                                            },
                                            {
                                                "code": 3,
                                                "text": "Worried",
                                                "element": "qdf:response"
                                            },
                                            {
                                                "code": 4,
                                                "text": "Sick",
                                                "element": "qdf:response"
                                            },
                                            {
                                                "code": 5,
                                                "text": "Tiring ",
                                                "element": "qdf:response"
                                            }
                                        ],
                                        "element": "qdf:response_group"
                                    },
                                    "element": "qdf:question",
                                    "export": [
                                        {
                                            "name": "KYS_Q3A"
                                        }
                                    ]
                                }
                            ],
                            "element": "qdf:section"
                        },
                        {
                            "type": "single",
                            "options": {
                                "sort_order": "desending",
                                "chart_layout": "5",
                                "chart_type": "bar",
                                "chart_color": "green"
                            },
                            "text": "{single order=\"randomize\"} You said that you $event P.E...<br/> Which ONE of the following is the main reason you like it? ",
                            "name": "KYS_Q4",
                            "responses": {
                                "items": [
                                    {
                                        "code": 1,
                                        "text": "I like getting outside/ not being in the classroom",
                                        "element": "qdf:response"
                                    },
                                    {
                                        "code": 2,
                                        "text": "I like my P.E. teacher",
                                        "element": "qdf:response"
                                    },
                                    {
                                        "code": 3,
                                        "text": "I’m good at it",
                                        "element": "qdf:response"
                                    },
                                    {
                                        "code": 4,
                                        "text": "I find the games/ sports fun",
                                        "element": "qdf:response"
                                    },
                                    {
                                        "code": 5,
                                        "text": "I find the games/ sports easy",
                                        "element": "qdf:response"
                                    }
                                ],
                                "element": "qdf:response_group"
                            },
                            "element": "qdf:question",
                            "export": [
                                {
                                    "name": "KYS_Q4"
                                }
                            ]
                        }
                    ],
                    "element": "qdf:page"
                }
            ],
            "element": "qdf:module"
        },
        {
            "name": "two",
            "children": [
                {
                    "name": "test",
                    "children": [
                        {
                            "type": "single",
                            "options": {
                                "sort_order": "asending",
                                "chart_layout": "5",
                                "chart_type": "bar",
                                "chart_color": "green"
                            },
                            "text": "{multiple order=\"randomize\"}Below are some feelings that people can have about doing P.E at school...<br/> Which of the following words describe how you feel about doing P.E. at school? (Please tick all that apply)   ",
                            "name": "KYS_Q5A",
                            "responses": {
                                "items": [
                                    {
                                        "code": 1,
                                        "text": "Energised/ Awake",
                                        "element": "qdf:response"
                                    },
                                    {
                                        "code": 2,
                                        "text": "Excited",
                                        "element": "qdf:response"
                                    },
                                    {
                                        "code": 3,
                                        "text": "Relaxed",
                                        "element": "qdf:response"
                                    },
                                    {
                                        "code": 4,
                                        "text": "Confident",
                                        "element": "qdf:response"
                                    },
                                    {
                                        "code": 5,
                                        "text": "Happy",
                                        "element": "qdf:response"
                                    }
                                ],
                                "element": "qdf:response_group"
                            },
                            "element": "qdf:question",
                            "export": [
                                {
                                    "name": "KYS_Q5A"
                                }
                            ]
                        },
                        {
                            "type": "single",
                            "options": {
                                "sort_order": "asending",
                                "chart_layout": "25",
                                "chart_type": "column",
                                "chart_color": "orange"
                            },
                            "text": "{multiple order=\"randomize\"} For the following question please do not be afraid to be as honest as possible, no one else will be told your personal answers. <br/> Which of the following have you EVER done in order to avoid taking part in P.E. at school? (Please tick all that apply) ",
                            "name": "KYS_Q6",
                            "responses": {
                                "items": [
                                    {
                                        "code": 1,
                                        "text": "Lied about forgetting my kit",
                                        "element": "qdf:response"
                                    },
                                    {
                                        "code": 2,
                                        "text": "Lied about an injury",
                                        "element": "qdf:response"
                                    },
                                    {
                                        "code": 3,
                                        "text": "Lied about feeling unwell",
                                        "element": "qdf:response"
                                    },
                                    {
                                        "code": 4,
                                        "text": "Faked a note from my parents/ guardian",
                                        "element": "qdf:response"
                                    },
                                    {
                                        "code": 5,
                                        "text": "Faked a note from my doctor/ health professional",
                                        "element": "qdf:response"
                                    }
                                ],
                                "element": "qdf:response_group"
                            },
                            "element": "qdf:question",
                            "export": [
                                {
                                    "name": "KYS_Q6"
                                }
                            ]
                        },
                        {
                            "type": "single",
                            "options": {
                                "sort_order": "asending",
                                "chart_layout": "5",
                                "chart_type": "bar",
                                "chart_color": "green"
                            },
                            "text": "{multiple order=\"randomize\"} For the following question, please think about the teacher who has taught you P.E. MOST recently and if you have more than one teacher teaching you P.E. please think about the one who has taught you most often. <br/>  Which of the following statements do you agree with? (Please tick all that apply)",
                            "name": "KYS_Q7",
                            "responses": {
                                "items": [
                                    {
                                        "code": 1,
                                        "text": "My P.E. teacher inspires me to do more exercise",
                                        "element": "qdf:response"
                                    },
                                    {
                                        "code": 2,
                                        "text": "My P.E. teacher motivates me to take part in P.E. lessons",
                                        "element": "qdf:response"
                                    },
                                    {
                                        "code": 3,
                                        "text": "My P.E. teacher scares me",
                                        "element": "qdf:response"
                                    },
                                    {
                                        "code": 4,
                                        "text": "My P.E. teacher makes P.E. boring",
                                        "element": "qdf:response"
                                    },
                                    {
                                        "code": 5,
                                        "text": "My P.E. teacher makes P.E. fun",
                                        "element": "qdf:response"
                                    },
                                    {
                                        "code": 6,
                                        "text": "My P.E. teacher makes P.E. difficult/ hard",
                                        "element": "qdf:response"
                                    }
                                ],
                                "element": "qdf:response_group"
                            },
                            "element": "qdf:question",
                            "export": [
                                {
                                    "name": "KYS_Q7"
                                }
                            ]
                        },
                        {
                            "type": "single",
                            "options": {
                                "sort_order": "asending",
                                "chart_layout": "5",
                                "chart_type": "bar",
                                "chart_color": "green"
                            },
                            "text": "{multiple order=\"randomize\"} What would make you like P.E. more? (Please tick all that apply)",
                            "name": "KYS_Q8",
                            "responses": {
                                "items": [
                                    {
                                        "code": 1,
                                        "text": "If the P.E. teacher was nicer to me",
                                        "element": "qdf:response"
                                    },
                                    {
                                        "code": 2,
                                        "text": "If more lessons took place indoors (e.g. in the sports hall or a gym)",
                                        "element": "qdf:response"
                                    },
                                    {
                                        "code": 3,
                                        "text": "If more lessons took place outdoors",
                                        "element": "qdf:response"
                                    },
                                    {
                                        "code": 4,
                                        "text": "If we did more interesting sports/ activities",
                                        "element": "qdf:response"
                                    },
                                    {
                                        "code": 5,
                                        "text": "If we had more choice of the types of sport we had to do",
                                        "element": "qdf:response"
                                    },
                                    {
                                        "code": 6,
                                        "text": "If we could wear what we wanted for the lesson",
                                        "element": "qdf:response"
                                    },
                                    {
                                        "options": {
                                            "fixed": true
                                        },
                                        "children": [
                                            {
                                                "export": [
                                                    {
                                                        "name": "KYS_Q8other"
                                                    }
                                                ],
                                                "name": "KYS_Q8other",
                                                "type": "open",
                                                "element": "qdf:question"
                                            }
                                        ],
                                        "code": 7,
                                        "text": "Other ",
                                        "element": "qdf:response"
                                    },
                                    {
                                        "options": {
                                            "xor": true,
                                            "fixed": true
                                        },
                                        "code": 8,
                                        "text": "Don’t know",
                                        "element": "qdf:response"
                                    },
                                    {
                                        "options": {
                                            "xor": true,
                                            "fixed": true
                                        },
                                        "code": 9,
                                        "text": "Not applicable – nothing would make me like P.E. more than I already do",
                                        "element": "qdf:response"
                                    }
                                ],
                                "element": "qdf:response_group"
                            },
                            "element": "qdf:question",
                            "export": [
                                {
                                    "name": "KYS_Q8"
                                }
                            ]
                        }
                    ],
                    "element": "qdf:page"
                }
            ],
            "element": "qdf:module"
        },
        {
            "children": [
                {
                    "children": [
                        {
                            "text": "​\n",
                            "element": "qdf:text"
                        }
                    ],
                    "element": "qdf:page"
                }
            ],
            "element": "qdf:module"
        }
    ],
    "panelid": 13,
    "created": "#2014-01-10T15:57:18.594000+00:00#",
    "typespec": {
        "src": "/questionnaires/typespecs/initial.qdf",
        "element": "qdf:typespec"
    },
    "qdf-ext:ext": [
        "meaningful-page-names"
    ],
    "element": "qdf:questionnaire",
    "builtin": {
        "src": "/questionnaires/builtins/initial.qdf",
        "element": "qdf:typedef"
    },
    "committer": "majeed.sahebzadha",
    "name": "Test_maj_demo_newG4_2",
    "commit message": "added a new module within a module"
}

你能分享一个字典的例子吗? - C.B.
预期的输出是什么? - aIKid
输出有点难以显示。但基本上,我将“选项”键的值存储在“子项”键中,并通过函数将其发送到另一个文件。但我只需要了解如何重复这个过程。 - Boosted_d16
3个回答

8
我会按照以下步骤进行处理:
def recursive(input, output=None):
    if output is None:
        output = {} # container to store results
    if 'children' in input:
        # do whatever, add things to output
        recursive(input['children'], output)
    return output

这种方式,output 字典在迭代的所有深度中都是可访问的,并在最后与所有内容一起被 return。这意味着您不必显式处理递归调用的 return 值。

根据您拥有的内容,它可能看起来更像:

def recursive(input, output=None):
    if output is None:
        output = {} # container to store results
    if 'children' in input:
        for child in input['children']:
            # do whatever, add things to output
            recursive(child, output)
    return output    

output可能是不同的容器(例如listset)。


这会对输出有任何增加吗?我感到困惑。 - Adam Smith
4
@adsmith:由于我们完全不知道原帖想输出什么,所以本答案将留待填写。 - Martijn Pieters
1
我的示例并没有明确地添加任何内容,但我不知道原帖想要提取什么!关键是同一个对象在所有递归调用之间共享,因此您可以将所有结果收集在一起。 - jonrsharpe
由于 OP 回复解释他想要每次递归中来自另一个键的值,因此这是首选的实现。 - Adam Smith

0
def recurseDict(dictionary):
    if "children" in dictionary:
        return recurseDict(dictionary['children'])
    else:
        return "Whatever you want to return here -- you didn't specify"

这将接受一个字典,检查其中是否有"children"键,然后递归遍历字典dictionary['children']等等,直到到达没有'children'键的点(例如,字典没有更多分支),进入else分支,你可以返回任何你想要的东西--你没有指定。

-1 -- 如果你想检查一个字典中是否存在某个键,使用 if "children" in dictionary.keys() 这种方式是完全错误的。 - Bakuriu
@MartijnPieters 哎呀!我对自递归函数有点生疏了。它们从来不像是正确的答案。@Bakurio:改成 if 'children' in dictionary -- 我总是记不住是否可以这样做,所以我倾向于构建一个键列表并检查它。 - Adam Smith
@Bakuriu 我想我可以这样做 try: return recurseDict(dictionary['children']) except KeyError: return "whatever OP wants" 如果你认为这更符合Pythonic。 - Adam Smith
不,你没有理解重点。检查字典中是否存在一个键的正确方法是用英语编写代码:key in dictionary。而不是调用.keys()方法。 - Bakuriu

-1

这里是一个递归遍历字典的示例方法,假设每个子项也是一个字典:

def compute(my_dict):
    if 'children' in my_dict:
        print my_dict['children']
        for child in my_dict['children']:
            compute(child)

如果my_dict不是可迭代对象,那么if 'children' in my_dict将会失败;如果my_dict不像字典一样,那么print my_dict['children']也会失败。你需要检查对象的类型(使用is_instance)或将这些表达式放在try ... except块中。我更喜欢鸭子类型,即使用try ... except。 - Steinar Lima
@SteinarLima 如果这个函数不会暴露给用户,那么除了字典之外,没有理由期望传递其他任何东西。如果你非常关心它,你可以将 def compute 的第一行写成 assert typeof(my_dict) == dict,这样如果你给它任何它无法处理的东西,它就会立即抛出一个 AssertionError - Adam Smith
那么对于我的字典 my_dict = {'children': ['children', 'of', 'men']},这是一个递归函数,因此仅仅初始的 my_dict 是一个字典是不够的,所有子项也需要是字典。 - Steinar Lima
是的,这就是数据结构应该设计的方式。当存在children键时,它应该包含子节点,而不是值。鸭子类型只有在必要时才应该使用。例如,对于外部数据结构,这可能是必要的,但如果您设计数据结构,则没有意义将子节点和值混合在不同级别的children键中。 - Simeon Visser

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接