如何迭代复杂的字典?

How to iterate complicated dictionary?

首先,我想让您知道,我没有在堆栈溢出中找到特定于我的问题的答案。所以征求你的建议。我想遍历下面的嵌套字典来创建下面给出的字符串。能否请教一下。

features = {
    "ptf_overall2": {
        "1": {
            "groupBy": {
                "1": {"column": "country"},
                "2": {"column": "measurement_group"},
                "3": {"column": "bpid"},
            }
        },
        "2": {
            "number_of_journeys_customer_eligible": {
                "operation": "countDistinct",
                "column": "journeyinstanceid",
            }
        },
        "3": {
            "number_of_journeys_customer_been_contacted": {
                "operation": "sum",
                "column": "journey_email_been_sent_flag",
            }
        },
    }
}

基本上我需要确定聚合操作和与之关联的那些列的顺序,然后将这些列附加到聚合操作中,如下所示。顺序对我来说很重要。

ptf_overall2.groupBy('country', 'measurement_group', 'bpid').

通过以下迭代,我得到如下错误

for i in features.get("ptf_overall2"):
    print(features.get("ptf_overall2")[i])
    for j in features.get("ptf_overall2")[i]:
        print(features.get("ptf_overall2")[j])

错误

KeyError: 'GroupBy'
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<command-2704284371067158> in <module>
      2   #print(features.get('ptf_overall2')[i])
      3   for j in features.get('ptf_overall2')[i]:
----> 4     print(features.get('ptf_overall2')[j])

KeyError: 'GroupBy'

也许是这样的。它应该适用于任何只有一个顶级键的字典,后跟一个字典的字典,其中一个应该有一个 groupBy 键。

features = {
    "ptf_overall2": {
        "1": {
            "groupBy": {
                "1": {"column": "country"},
                "2": {"column": "measurement_group"},
                "3": {"column": "bpid"},
            }
        },
        "2": {
            "number_of_journeys_customer_eligible": {
                "operation": "countDistinct",
                "column": "journeyinstanceid",
            }
        },
        "3": {
            "number_of_journeys_customer_been_contacted": {
                "operation": "sum",
                "column": "journey_email_been_sent_flag",
            }
        },
    }
}


def get_group_by(d):
    assert (
        len(d) == 1
    ), "dictionary has more than 1 top-level key"
    key = next(iter(d))  # find first (only) key
    for value in d[key].values():
        if "groupBy" in value:  # found the `groupBy` item...
            columns = [
                c["column"]
                for index, c in sorted(value["groupBy"].items())
            ]
            return f"{key}.groupBy{tuple(columns)}"


print(get_group_by(features))

输出如预期的那样

ptf_overall2.groupBy('country', 'measurement_group', 'bpid')