如何在Python中展平对象列表

问题描述 投票:0回答:1

我有一个对象列表:

[
  {
    "person": "abc",
    "city": "united states",
    "facebooklink": "link",
    "address": "united states",
    "united states": [
      {
        "person": "cdf",
        "city": "ohio",
        "facebooklink": "link",
        "address": "united states/ohio",
        "ohio": [
          {
            "person": "efg",
            "city": "clevland",
            "facebooklink": "link",
            "address": "united states/ohio/clevland",
            "clevland": [
              {
                "person": "jkl",
                "city": "Street A",
                "facebooklink": "link",
                "address": "united states/ohio/clevland/Street A",
                "Street A": [
                  {
                    "person": "jkl",
                    "city": "House 1",
                    "facebooklink": "link",
                    "address": "united states/ohio/clevland/Street A/House 1"
                  }
                ]
              }
            ]
          },
          {
            "person": "ghi",
            "city": "columbus",
            "facebooklink": "link",
            "address": "united states/ohio/columbus"
          }
        ]
      },
      {
        "person": "abc",
        "city": "washington",
        "facebooklink": "link",
        "address": "united states/washington"
      }
    ]
  }
]

我怎样才能把它压平

[
  {
    "person": "abc",
    "city": "united states",
    "facebooklink": "link",
    "address": "united states"
  },
  {
    "person": "cdf",
    "city": "ohio",
    "facebooklink": "link",
    "address": "united states/ohio"
  },
  {
    "person": "efg",
    "city": "clevland",
    "facebooklink": "link",
    "address": "united states/ohio/clevland"
  },
  {
    "person": "jkl",
    "city": "Street A",
    "facebooklink": "link",
    "address": "united states/ohio/clevland/Street A"
  },
  {
    "person": "jkl",
    "city": "House 1",
    "facebooklink": "link",
    "address": "united states/ohio/clevland/Street A/House 1"
  },
  {
    "person": "ghi",
    "city": "columbus",
    "facebooklink": "link",
    "address": "united states/ohio/columbus"
  },
  {
    "person": "abc",
    "city": "washington",
    "facebooklink": "link",
    "address": "united states/washington"
  }
]

我正在尝试使用 flatten_json 中的 flatten 来实现相同的目的

python json recursion iteration flatten
1个回答
0
投票

这是一种定义“自定义”递归函数的方法,该函数采用两个参数:对象列表(或单个对象)和一个用于累积扁平对象的列表,其中我们为每个对象创建一个新字典 - 仅包含我们想要的键保留,之后您显然会将其附加到累加器列表中,然后检查该对象是否包含嵌套列表,这可以通过迭代对象的键来完成,如果存在嵌套列表,则使用此递归调用该函数列表。

def flatten_objects(objects, flattened=None):
    if flattened is None:
        flattened = []

    # Convert a single dict to a list for consistency
    if isinstance(objects, dict):
        objects = [objects]

    for obj in objects:
        # assuming keys exist, extract them
        flat_obj = {key: obj[key] for key in ['person', 'city', 'facebooklink', 'address']}
        flattened.append(flat_obj)

        # Recursively process any nested lists
        for key, value in obj.items():
            if isinstance(value, list):  
                flatten_objects(value, flattened)

    return flattened

nested_objects= [
  {
    "person": "abc",
    "city": "united states",
    "facebooklink": "link",
    "address": "united states",
    "united states": [
      {
        "person": "cdf",
        "city": "ohio",
        "facebooklink": "link",
        "address": "united states/ohio",
        "ohio": [
          {
            "person": "efg",
            "city": "clevland",
            "facebooklink": "link",
            "address": "united states/ohio/clevland",
            "clevland": [
              {
                "person": "jkl",
                "city": "Street A",
                "facebooklink": "link",
                "address": "united states/ohio/clevland/Street A",
                "Street A": [
                  {
                    "person": "jkl",
                    "city": "House 1",
                    "facebooklink": "link",
                    "address": "united states/ohio/clevland/Street A/House 1"
                  }
                ]
              }
            ]
          },
          {
            "person": "ghi",
            "city": "columbus",
            "facebooklink": "link",
            "address": "united states/ohio/columbus"
          }
        ]
      },
      {
        "person": "abc",
        "city": "washington",
        "facebooklink": "link",
        "address": "united states/washington"
      }
    ]
  }
]
flattened_list = flatten_objects(nested_objects)
print(flattened_list)

运行结果(输出如下):

[
   {
      "person":"abc",
      "city":"united states",
      "facebooklink":"link",
      "address":"united states"
   },
   {
      "person":"cdf",
      "city":"ohio",
      "facebooklink":"link",
      "address":"united states/ohio"
   },
   {
      "person":"efg",
      "city":"clevland",
      "facebooklink":"link",
      "address":"united states/ohio/clevland"
   },
   {
      "person":"jkl",
      "city":"Street A",
      "facebooklink":"link",
      "address":"united states/ohio/clevland/Street A"
   },
   {
      "person":"jkl",
      "city":"House 1",
      "facebooklink":"link",
      "address":"united states/ohio/clevland/Street A/House 1"
   },
   {
      "person":"ghi",
      "city":"columbus",
      "facebooklink":"link",
      "address":"united states/ohio/columbus"
   },
   {
      "person":"abc",
      "city":"washington",
      "facebooklink":"link",
      "address":"united states/washington"
   }
]
© www.soinside.com 2019 - 2024. All rights reserved.