Python获取包含特定字段的dict条目

时间:2017-12-04 11:12:54

标签: python dictionary recursion

我在Python中有一个嵌套的dict,包含像

这样的YAML结构
  - id: left_time
    type: u2
    doc: Time left

我希望获得像{id:doc}这样的对。对于这个例子,我希望它是:{“left_time”:“剩下的时间”}。问题是我需要递归地浏览它们。

我的尝试是

def get_dict_recursively(search_dict, field):
    fields_found = []

    name = ""

    for key, value in search_dict.items():

        if key == "id":
            name = value

        if key == field:
            fields_found.append({name: value})

        elif isinstance(value, dict):
            results = get_dict_recursively(value, field)
            for result in results:
                fields_found.append({name: result})

        elif isinstance(value, list):
            for item in value:
                if isinstance(item, dict):
                    more_results = get_dict_recursively(item, field)
                    for another_result in more_results:
                        fields_found.append({name: another_result})

    return fields_found

称之为

get_dict_recursively(dict, "doc")

其中

dict = {
  meta:
    id: foo
    title: Foo
  types:
    data:
      seq:
        - id: left_time
          type: u2
          doc: Time left
    gps:
      seq:
        - id: gps_st
          type: b2
        - id: sats
          type: b6
          doc: Number of satellites
}

有一个错误,但我找不到它。

1 个答案:

答案 0 :(得分:0)

让我们首先将您的示例数据说明为dict

data = {
    "meta": {
        "id": "foo",
        "title": "Foo"
    },
    "types": {
        "data": {
            "seq": [
                {
                    "id": "left_time",
                    "type": "u2",
                    "doc": "Time left"
                }
            ]
        },
        "gps": {
            "seq": [
                {
                    "id": "gps_st",
                    "type": "b2"
                },
                {
                    "id": "sats",
                    "type": "b6",
                    "doc": "Number of satellites"
                }
            ]
        }
    }
}

接下来,我们可以简化您的递归函数,如下所示:

def extract_docs(data):
    result = []
    if isinstance(data, list):
        for d in data:
            result += extract_docs(d)
    elif isinstance(data, dict):
        if "id" in data and "doc" in data:
            result.append((data["id"], data["doc"]))
        else:
            for d in data.values():
                result += extract_docs(d)
    return result

这样你就得到了

>>> dict(extract_docs(data))
{'sats': 'Number of satellites', 'left_time': 'Time left'}