我有一个dicts列表,其中包含一个字段中的另一个列表.我想"扁平化"该列表,因此它为每个子元素提供了一个字段(或一些字段)从父项复制到其中.例:
来源数据:
[
{
"name": "A",
"foo": "x",
"bar": 1,
"subelements": [
{
"baz": "xyz",
"foobar": "abc"
},
{
"baz": "zzz",
"foobar": "def"
}
]
},
{
"name": "B",
"foo": "Y",
"bar": 4,
"subelements": [
{
"baz": "yyy",
"foobar": "aaa"
},
{
"baz": "xxx",
"foobar": "bbb"
},
{
"baz": "www",
"foobar": "bbb"
}
]
}
]
Run Code Online (Sandbox Code Playgroud)
预期结果:
[
{
"baz": "xyz",
"foobar": "abc",
"foo": "x"
},
{
"baz": "zzz",
"foobar": "def",
"foo": "x"
},
{
"baz": "yyy",
"foobar": "aaa",
"foo": "Y"
},
{
"baz": "xxx",
"foobar": "bbb",
"foo": "Y"
},
{
"baz": "www",
"foobar": "bbb",
"foo": "Y"
}
]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
248 次 |
| 最近记录: |