将Django中的嵌套列表转换为扁平列表的方法,Python

huangapple go评论71阅读模式
英文:

change an imbricated list to a flat list in django, python

问题

我想在输出中获得我的类别列表的扁平列表,但这段代码没有做到。它仍然显示嵌套列表中的类别。有人可以帮助我吗?

这是代码:

@api_view(['GET'])
def AllCategoriesList(request):
    categories = Categories.objects.filter(parent=None).prefetch_related(
        Prefetch('children', queryset=Categories.objects.all(), to_attr='subcategories')
    )
    
    def flatten_list(lst):
        flat_lst = []
        for item in lst:
            if isinstance(item, list):
                flat_lst.extend(flatten_list(item))
            else:
                flat_lst.append(item)
		
        return flat_lst
    
    category_list = []
    for category in categories:
        category_list.append(category)
        category_list.extend(category.children.all())

    flattened_list = flatten_list(category_list)

    serializer = CategorieSerializer(flattened_list, many=True)
    return Response(serializer.data)

我尝试修改我的代码来使用flatten_list函数,但它不起作用。

这是输出:

 { "id": 1,
    "image_couverture": null,
    "children": [{ "id": 2,
        "image_couverture": null,
        "children": [],
        "nom_categorie": "nouveau categorie",
        "descriptions": "categorie fille",
        "slug": "nouveau-categorie",
        "created_at": "2023-03-04T08:08:35.667959Z",
        "updated_at": null,
        "parent": 1
      }
    ],
    "nom_categorie": "Test categorie",
    "descriptions": "test categorie",
    "slug": "test-categorie",
    "created_at": "2023-03-04T06:43:42.628255Z",
    "updated_at": null,
    "parent": null
  },
  {
    "id": 2,
    "image_couverture": null,
    "children": [],
    "nom_categorie": "nouveau categorie",
    "descriptions": "categorie fille",
    "slug": "nouveau-categorie",
    "created_at": "2023-03-04T08:08:35.667959Z",
    "updated_at": null,
    "parent": 1
  },
  {
    "id": 3,
    "image_couverture": null,
    "children": [
      {
        "id": 4,
        "image_couverture": null,
        "children": [],
        "nom_categorie": "Boissons",
        "descriptions": "Test description",
        "slug": "boissons",
        "created_at": "2023-03-06T10:13:39.229660Z",
        "updated_at": null,
        "parent": 3
      }
    ],
    "nom_categorie": "Alimentation & vins",
    "descriptions": "Test Description",
    "slug": "alimentation-vins",
    "created_at": "2023-03-06T10:06:24.492310Z",
    "updated_at": null,
    "parent": null
  },
  {
    "id": 4,
    "image_couverture": null,
    "children": [],
    "nom_categorie": "Boissons",
    "descriptions": "Test description",
    "slug": "boissons",
    "created_at": "2023-03-06T10:13:39.229660Z",
    "updated_at": null,
    "parent": 3
  },
  {
    "id": 5,
    "image_couverture": null,
    "children": [
      {
        "id": 13,
        "image_couverture": null,
        "children": [],
        "nom_categorie": "spa",
        "descriptions": "hotel bla",
        "slug": "spa",
        "created_at": "2023-03-07T16:54:15.145494Z",
        "updated_at": null,
        "parent": 5
      }
    ],
    "nom_categorie": "VOYAGES et LOCATIONS",
    "descriptions": "Test",
    "slug": "voyages-et-locations",
    "created_at": "2023-03-06T10:58:44.996614Z",
    "updated_at": null,
    "parent": null
  },
  {
    "id": 13,
    "image_couverture": null,
    "children": [],
    "nom_categorie": "spa",
    "descriptions": "hotel bla",
    "slug": "spa",
    "created_at": "2023-03-07T16:54:15.145494Z",
    "updated_at": null,
    "parent": 5
  },
  {
    "id": 6,
    "image_couverture": null,
    "children": [
      {
        "id": 12,
        "image_couverture": null,
        "children": [],
        "nom_categorie": "Ballon",
        "descriptions": "gfjfm jtukj gfdf",
        "slug": "ballon",
        "created_at": "2023-03-06T18:28:25.832572Z",
        "updated_at": null,
        "parent": 6
      }
    ],
    "nom_categorie": "SPORTS et FITNESS",
    "descriptions": "Test",
    "slug": "sports-et-fitness",
    "created_at": "2023-03-06T10:59:26.929241Z",
    "updated_at": null,
    "parent": null
  },
  {
    "id": 12,
    "image_couverture": null,
    "children": [],
    "nom_categorie": "Ballon",
    "descriptions": "gfjfm jtukj gfdf",
    "slug": "ballon",
    "created_at": "2023-03-06T18:28:25.832572Z",
    "updated_at": null,
    "parent": 6
  },
  {
    "id": 7,
    "image_couverture": null,
    "children": [
      {
        "id": 8,
        "image_couverture": null,
        "children": [],
        "nom_categorie": "Chaussures femme",
        "descriptions": "Test",
        "slug": "chaussures-femme",
        "created_at": "2023-03-06T11:15:08.421713Z",
        "updated_at": null,
        "parent": 7
      }
    ],
    "nom_categorie": "MODE FEMME",
    "descriptions": "Test",
    "slug": "mode-femme",
    "created_at": "2023-03-06T11:13:09.847437Z",
    "updated_at": null,
    "parent": null
  },
  {
    "id": 8,
    "image_couverture": null,


<details>
<summary>英文:</summary>

I want a flat list for my categories list in output, but this code doesn&#39;t do that. It still shows the categories in imbricated list. Can someone help me?

This is the code: 
```python
@api_view([&#39;GET&#39;])
def AllCategoriesList(request):
    categories = Categories.objects.filter(parent=None).prefetch_related(
        Prefetch(&#39;children&#39;, queryset=Categories.objects.all(), to_attr=&#39;subcategories&#39;)
    )
    
    def flatten_list(lst):
        flat_lst = []
        for item in lst:
            if isinstance(item, list):
                flat_lst.extend(flatten_list(item))
            else:
                flat_lst.append(item)
		
        return flat_lst
    
    category_list = []
    for category in categories:
        category_list.append(category)
        category_list.extend(category.children.all())

    flattened_list = flatten_list(category_list)

    serializer = CategorieSerializer(flattened_list, many=True)
    return Response(serializer.data)

I tried to change my code to use the flatten_list function, but it doesn't work.

This is the output:

 { &quot;id&quot;: 1,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [{ &quot;id&quot;: 2,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [],
&quot;nom_categorie&quot;: &quot;nouveau categorie&quot;,
&quot;descriptions&quot;: &quot;categorie fille&quot;,
&quot;slug&quot;: &quot;nouveau-categorie&quot;,
&quot;created_at&quot;: &quot;2023-03-04T08:08:35.667959Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: 1
}
],
&quot;nom_categorie&quot;: &quot;Test categorie&quot;,
&quot;descriptions&quot;: &quot;test categorie&quot;,
&quot;slug&quot;: &quot;test-categorie&quot;,
&quot;created_at&quot;: &quot;2023-03-04T06:43:42.628255Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: null
},
{
&quot;id&quot;: 2,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [],
&quot;nom_categorie&quot;: &quot;nouveau categorie&quot;,
&quot;descriptions&quot;: &quot;categorie fille&quot;,
&quot;slug&quot;: &quot;nouveau-categorie&quot;,
&quot;created_at&quot;: &quot;2023-03-04T08:08:35.667959Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: 1
},
{
&quot;id&quot;: 3,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [
{
&quot;id&quot;: 4,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [],
&quot;nom_categorie&quot;: &quot;Boissons&quot;,
&quot;descriptions&quot;: &quot;Test description&quot;,
&quot;slug&quot;: &quot;boissons&quot;,
&quot;created_at&quot;: &quot;2023-03-06T10:13:39.229660Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: 3
}
],
&quot;nom_categorie&quot;: &quot;Alimentation &amp; vins&quot;,
&quot;descriptions&quot;: &quot;Test Description&quot;,
&quot;slug&quot;: &quot;alimentation-vins&quot;,
&quot;created_at&quot;: &quot;2023-03-06T10:06:24.492310Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: null
},
{
&quot;id&quot;: 4,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [],
&quot;nom_categorie&quot;: &quot;Boissons&quot;,
&quot;descriptions&quot;: &quot;Test description&quot;,
&quot;slug&quot;: &quot;boissons&quot;,
&quot;created_at&quot;: &quot;2023-03-06T10:13:39.229660Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: 3
},
{
&quot;id&quot;: 5,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [
{
&quot;id&quot;: 13,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [],
&quot;nom_categorie&quot;: &quot;spa&quot;,
&quot;descriptions&quot;: &quot;hotel bla&quot;,
&quot;slug&quot;: &quot;spa&quot;,
&quot;created_at&quot;: &quot;2023-03-07T16:54:15.145494Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: 5
}
],
&quot;nom_categorie&quot;: &quot;VOYAGES et LOCATIONS&quot;,
&quot;descriptions&quot;: &quot;Test&quot;,
&quot;slug&quot;: &quot;voyages-et-locations&quot;,
&quot;created_at&quot;: &quot;2023-03-06T10:58:44.996614Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: null
},
{
&quot;id&quot;: 13,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [],
&quot;nom_categorie&quot;: &quot;spa&quot;,
&quot;descriptions&quot;: &quot;hotel bla&quot;,
&quot;slug&quot;: &quot;spa&quot;,
&quot;created_at&quot;: &quot;2023-03-07T16:54:15.145494Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: 5
},
{
&quot;id&quot;: 6,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [
{
&quot;id&quot;: 12,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [],
&quot;nom_categorie&quot;: &quot;Ballon&quot;,
&quot;descriptions&quot;: &quot;gfjfm jtukj gfdf&quot;,
&quot;slug&quot;: &quot;ballon&quot;,
&quot;created_at&quot;: &quot;2023-03-06T18:28:25.832572Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: 6
}
],
&quot;nom_categorie&quot;: &quot;SPORTS et FITNESS&quot;,
&quot;descriptions&quot;: &quot;Test&quot;,
&quot;slug&quot;: &quot;sports-et-fitness&quot;,
&quot;created_at&quot;: &quot;2023-03-06T10:59:26.929241Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: null
},
{
&quot;id&quot;: 12,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [],
&quot;nom_categorie&quot;: &quot;Ballon&quot;,
&quot;descriptions&quot;: &quot;gfjfm jtukj gfdf&quot;,
&quot;slug&quot;: &quot;ballon&quot;,
&quot;created_at&quot;: &quot;2023-03-06T18:28:25.832572Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: 6
},
{
&quot;id&quot;: 7,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [
{
&quot;id&quot;: 8,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [],
&quot;nom_categorie&quot;: &quot;Chaussures femme&quot;,
&quot;descriptions&quot;: &quot;Test&quot;,
&quot;slug&quot;: &quot;chaussures-femme&quot;,
&quot;created_at&quot;: &quot;2023-03-06T11:15:08.421713Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: 7
}
],
&quot;nom_categorie&quot;: &quot;MODE FEMME&quot;,
&quot;descriptions&quot;: &quot;Test&quot;,
&quot;slug&quot;: &quot;mode-femme&quot;,
&quot;created_at&quot;: &quot;2023-03-06T11:13:09.847437Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: null
},
{
&quot;id&quot;: 8,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [],
&quot;nom_categorie&quot;: &quot;Chaussures femme&quot;,
&quot;descriptions&quot;: &quot;Test&quot;,
&quot;slug&quot;: &quot;chaussures-femme&quot;,
&quot;created_at&quot;: &quot;2023-03-06T11:15:08.421713Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: 7
},
{
&quot;id&quot;: 9,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [
{
&quot;id&quot;: 10,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [],
&quot;nom_categorie&quot;: &quot;tafita-1&quot;,
&quot;descriptions&quot;: &quot;hgjjgj&quot;,
&quot;slug&quot;: &quot;tafita1&quot;,
&quot;created_at&quot;: &quot;2023-03-06T18:15:23.355740Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: 9
}
],
&quot;nom_categorie&quot;: &quot;tafita&quot;,
&quot;descriptions&quot;: &quot;nljbk&quot;,
&quot;slug&quot;: &quot;tafita&quot;,
&quot;created_at&quot;: &quot;2023-03-06T18:14:58.259709Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: null
},
{
&quot;id&quot;: 10,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [],
&quot;nom_categorie&quot;: &quot;tafita-1&quot;,
&quot;descriptions&quot;: &quot;hgjjgj&quot;,
&quot;slug&quot;: &quot;tafita1&quot;,
&quot;created_at&quot;: &quot;2023-03-06T18:15:23.355740Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: 9
},
{
&quot;id&quot;: 11,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [],
&quot;nom_categorie&quot;: &quot;Tech&quot;,
&quot;descriptions&quot;: &quot;gd hgjkjg th&quot;,
&quot;slug&quot;: &quot;tech&quot;,
&quot;created_at&quot;: &quot;2023-03-06T18:25:00.949297Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: null
},
{
&quot;id&quot;: 14,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [],
&quot;nom_categorie&quot;: &quot;Hotel34&quot;,
&quot;descriptions&quot;: &quot;DJGJ JGKJ&quot;,
&quot;slug&quot;: &quot;hotel34&quot;,
&quot;created_at&quot;: &quot;2023-03-07T16:57:03.666177Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: null
}
]

答案1

得分: 0

以下是翻译的代码部分:

def flatten_list(data):
    flat_lst = []

    if isinstance(data, list):
        for item in data:
            flat_lst.append(item)
            flat_lst.extend(flatten_list(item))
        return flat_lst

    if isinstance(data, dict):
        ## ----------------------
        ## 使用项目的副本,以便我们不直接
        ## 迭代字典
        ## ----------------------
        for key, value in list(data.items()):
            flat_lst.extend(flatten_list(value))

            ### ----------------------
            ### 你不想要 "children": []
            ### ----------------------
            if isinstance(value, list):
                del data[key]
            ### ----------------------
        ## ----------------------

    return flat_lst

data = json.loads(data_raw)
data = flatten_list(data)

## ---------------
## 仅唯一的项目(按id)
## ---------------
data = list({item["id"]: item for item in data}.values())
## ---------------

print(json.dumps(data, indent=4))

希望这对您有所帮助。

英文:

If you seek to flatted/promote all nested dictionaries, you might look at something like this:

Given input data as:

data_raw = &quot;&quot;&quot;
[
{
&quot;id&quot;: 1,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [
{
&quot;id&quot;: 2,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [],
&quot;nom_categorie&quot;: &quot;nouveau categorie&quot;,
&quot;descriptions&quot;: &quot;categorie fille&quot;,
&quot;slug&quot;: &quot;nouveau-categorie&quot;,
&quot;created_at&quot;: &quot;2023-03-04T08:08:35.667959Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: 1
}
],
&quot;nom_categorie&quot;: &quot;Test categorie&quot;,
&quot;descriptions&quot;: &quot;test categorie&quot;,
&quot;slug&quot;: &quot;test-categorie&quot;,
&quot;created_at&quot;: &quot;2023-03-04T06:43:42.628255Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: null
},
{
&quot;id&quot;: 2,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [],
&quot;nom_categorie&quot;: &quot;nouveau categorie&quot;,
&quot;descriptions&quot;: &quot;categorie fille&quot;,
&quot;slug&quot;: &quot;nouveau-categorie&quot;,
&quot;created_at&quot;: &quot;2023-03-04T08:08:35.667959Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: 1
},
{
&quot;id&quot;: 3,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [
{
&quot;id&quot;: 4,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [],
&quot;nom_categorie&quot;: &quot;Boissons&quot;,
&quot;descriptions&quot;: &quot;Test description&quot;,
&quot;slug&quot;: &quot;boissons&quot;,
&quot;created_at&quot;: &quot;2023-03-06T10:13:39.229660Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: 3
}
],
&quot;nom_categorie&quot;: &quot;Alimentation &amp; vins&quot;,
&quot;descriptions&quot;: &quot;Test Description&quot;,
&quot;slug&quot;: &quot;alimentation-vins&quot;,
&quot;created_at&quot;: &quot;2023-03-06T10:06:24.492310Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: null
},
{
&quot;id&quot;: 4,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [],
&quot;nom_categorie&quot;: &quot;Boissons&quot;,
&quot;descriptions&quot;: &quot;Test description&quot;,
&quot;slug&quot;: &quot;boissons&quot;,
&quot;created_at&quot;: &quot;2023-03-06T10:13:39.229660Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: 3
},
{
&quot;id&quot;: 5,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [
{
&quot;id&quot;: 13,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [],
&quot;nom_categorie&quot;: &quot;spa&quot;,
&quot;descriptions&quot;: &quot;hotel bla&quot;,
&quot;slug&quot;: &quot;spa&quot;,
&quot;created_at&quot;: &quot;2023-03-07T16:54:15.145494Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: 5
}
],
&quot;nom_categorie&quot;: &quot;VOYAGES et LOCATIONS&quot;,
&quot;descriptions&quot;: &quot;Test&quot;,
&quot;slug&quot;: &quot;voyages-et-locations&quot;,
&quot;created_at&quot;: &quot;2023-03-06T10:58:44.996614Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: null
},
{
&quot;id&quot;: 13,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [],
&quot;nom_categorie&quot;: &quot;spa&quot;,
&quot;descriptions&quot;: &quot;hotel bla&quot;,
&quot;slug&quot;: &quot;spa&quot;,
&quot;created_at&quot;: &quot;2023-03-07T16:54:15.145494Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: 5
},
{
&quot;id&quot;: 6,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [
{
&quot;id&quot;: 12,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [],
&quot;nom_categorie&quot;: &quot;Ballon&quot;,
&quot;descriptions&quot;: &quot;gfjfm jtukj gfdf&quot;,
&quot;slug&quot;: &quot;ballon&quot;,
&quot;created_at&quot;: &quot;2023-03-06T18:28:25.832572Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: 6
}
],
&quot;nom_categorie&quot;: &quot;SPORTS et FITNESS&quot;,
&quot;descriptions&quot;: &quot;Test&quot;,
&quot;slug&quot;: &quot;sports-et-fitness&quot;,
&quot;created_at&quot;: &quot;2023-03-06T10:59:26.929241Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: null
},
{
&quot;id&quot;: 12,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [],
&quot;nom_categorie&quot;: &quot;Ballon&quot;,
&quot;descriptions&quot;: &quot;gfjfm jtukj gfdf&quot;,
&quot;slug&quot;: &quot;ballon&quot;,
&quot;created_at&quot;: &quot;2023-03-06T18:28:25.832572Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: 6
},
{
&quot;id&quot;: 7,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [
{
&quot;id&quot;: 8,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [],
&quot;nom_categorie&quot;: &quot;Chaussures femme&quot;,
&quot;descriptions&quot;: &quot;Test&quot;,
&quot;slug&quot;: &quot;chaussures-femme&quot;,
&quot;created_at&quot;: &quot;2023-03-06T11:15:08.421713Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: 7
}
],
&quot;nom_categorie&quot;: &quot;MODE FEMME&quot;,
&quot;descriptions&quot;: &quot;Test&quot;,
&quot;slug&quot;: &quot;mode-femme&quot;,
&quot;created_at&quot;: &quot;2023-03-06T11:13:09.847437Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: null
},
{
&quot;id&quot;: 8,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [],
&quot;nom_categorie&quot;: &quot;Chaussures femme&quot;,
&quot;descriptions&quot;: &quot;Test&quot;,
&quot;slug&quot;: &quot;chaussures-femme&quot;,
&quot;created_at&quot;: &quot;2023-03-06T11:15:08.421713Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: 7
},
{
&quot;id&quot;: 9,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [
{
&quot;id&quot;: 10,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [],
&quot;nom_categorie&quot;: &quot;tafita-1&quot;,
&quot;descriptions&quot;: &quot;hgjjgj&quot;,
&quot;slug&quot;: &quot;tafita1&quot;,
&quot;created_at&quot;: &quot;2023-03-06T18:15:23.355740Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: 9
}
],
&quot;nom_categorie&quot;: &quot;tafita&quot;,
&quot;descriptions&quot;: &quot;nljbk&quot;,
&quot;slug&quot;: &quot;tafita&quot;,
&quot;created_at&quot;: &quot;2023-03-06T18:14:58.259709Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: null
},
{
&quot;id&quot;: 10,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [],
&quot;nom_categorie&quot;: &quot;tafita-1&quot;,
&quot;descriptions&quot;: &quot;hgjjgj&quot;,
&quot;slug&quot;: &quot;tafita1&quot;,
&quot;created_at&quot;: &quot;2023-03-06T18:15:23.355740Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: 9
},
{
&quot;id&quot;: 11,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [],
&quot;nom_categorie&quot;: &quot;Tech&quot;,
&quot;descriptions&quot;: &quot;gd hgjkjg th&quot;,
&quot;slug&quot;: &quot;tech&quot;,
&quot;created_at&quot;: &quot;2023-03-06T18:25:00.949297Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: null
},
{
&quot;id&quot;: 14,
&quot;image_couverture&quot;: null,
&quot;children&quot;: [],
&quot;nom_categorie&quot;: &quot;Hotel34&quot;,
&quot;descriptions&quot;: &quot;DJGJ JGKJ&quot;,
&quot;slug&quot;: &quot;hotel34&quot;,
&quot;created_at&quot;: &quot;2023-03-07T16:57:03.666177Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: null
}
]
&quot;&quot;&quot;

Then you can:

def flatten_list(data):
    flat_lst = []

    if isinstance(data, list):
        for item in data:
            flat_lst.append(item)
            flat_lst.extend(flatten_list(item))
        return flat_lst

    if isinstance(data, dict):
        ## ----------------------
        ## Use a copy of the items so we are not directly
        ## iterating over the dictionary
        ## ----------------------
        for key, value in list(data.items()):
            flat_lst.extend(flatten_list(value))

            ### ----------------------
            ### you don&#39;t want children&quot;: []
            ### ----------------------
            if isinstance(value, list):
                del data[key]
            ### ----------------------
        ## ----------------------
    
    return flat_lst

data = json.loads(data_raw)
data = flatten_list(data)

## ---------------
## just the unique items (by id)
## ---------------
data = list({item[&quot;id&quot;]: item for item in data}.values())
## ---------------

print(json.dumps(data, indent=4))

That should give you:

[
{
&quot;id&quot;: 1,
&quot;image_couverture&quot;: null,
&quot;nom_categorie&quot;: &quot;Test categorie&quot;,
&quot;descriptions&quot;: &quot;test categorie&quot;,
&quot;slug&quot;: &quot;test-categorie&quot;,
&quot;created_at&quot;: &quot;2023-03-04T06:43:42.628255Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: null
},
{
&quot;id&quot;: 2,
&quot;image_couverture&quot;: null,
&quot;nom_categorie&quot;: &quot;nouveau categorie&quot;,
&quot;descriptions&quot;: &quot;categorie fille&quot;,
&quot;slug&quot;: &quot;nouveau-categorie&quot;,
&quot;created_at&quot;: &quot;2023-03-04T08:08:35.667959Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: 1
},
{
&quot;id&quot;: 3,
&quot;image_couverture&quot;: null,
&quot;nom_categorie&quot;: &quot;Alimentation &amp; vins&quot;,
&quot;descriptions&quot;: &quot;Test Description&quot;,
&quot;slug&quot;: &quot;alimentation-vins&quot;,
&quot;created_at&quot;: &quot;2023-03-06T10:06:24.492310Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: null
},
{
&quot;id&quot;: 4,
&quot;image_couverture&quot;: null,
&quot;nom_categorie&quot;: &quot;Boissons&quot;,
&quot;descriptions&quot;: &quot;Test description&quot;,
&quot;slug&quot;: &quot;boissons&quot;,
&quot;created_at&quot;: &quot;2023-03-06T10:13:39.229660Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: 3
},
{
&quot;id&quot;: 5,
&quot;image_couverture&quot;: null,
&quot;nom_categorie&quot;: &quot;VOYAGES et LOCATIONS&quot;,
&quot;descriptions&quot;: &quot;Test&quot;,
&quot;slug&quot;: &quot;voyages-et-locations&quot;,
&quot;created_at&quot;: &quot;2023-03-06T10:58:44.996614Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: null
},
{
&quot;id&quot;: 13,
&quot;image_couverture&quot;: null,
&quot;nom_categorie&quot;: &quot;spa&quot;,
&quot;descriptions&quot;: &quot;hotel bla&quot;,
&quot;slug&quot;: &quot;spa&quot;,
&quot;created_at&quot;: &quot;2023-03-07T16:54:15.145494Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: 5
},
{
&quot;id&quot;: 6,
&quot;image_couverture&quot;: null,
&quot;nom_categorie&quot;: &quot;SPORTS et FITNESS&quot;,
&quot;descriptions&quot;: &quot;Test&quot;,
&quot;slug&quot;: &quot;sports-et-fitness&quot;,
&quot;created_at&quot;: &quot;2023-03-06T10:59:26.929241Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: null
},
{
&quot;id&quot;: 12,
&quot;image_couverture&quot;: null,
&quot;nom_categorie&quot;: &quot;Ballon&quot;,
&quot;descriptions&quot;: &quot;gfjfm jtukj gfdf&quot;,
&quot;slug&quot;: &quot;ballon&quot;,
&quot;created_at&quot;: &quot;2023-03-06T18:28:25.832572Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: 6
},
{
&quot;id&quot;: 7,
&quot;image_couverture&quot;: null,
&quot;nom_categorie&quot;: &quot;MODE FEMME&quot;,
&quot;descriptions&quot;: &quot;Test&quot;,
&quot;slug&quot;: &quot;mode-femme&quot;,
&quot;created_at&quot;: &quot;2023-03-06T11:13:09.847437Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: null
},
{
&quot;id&quot;: 8,
&quot;image_couverture&quot;: null,
&quot;nom_categorie&quot;: &quot;Chaussures femme&quot;,
&quot;descriptions&quot;: &quot;Test&quot;,
&quot;slug&quot;: &quot;chaussures-femme&quot;,
&quot;created_at&quot;: &quot;2023-03-06T11:15:08.421713Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: 7
},
{
&quot;id&quot;: 9,
&quot;image_couverture&quot;: null,
&quot;nom_categorie&quot;: &quot;tafita&quot;,
&quot;descriptions&quot;: &quot;nljbk&quot;,
&quot;slug&quot;: &quot;tafita&quot;,
&quot;created_at&quot;: &quot;2023-03-06T18:14:58.259709Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: null
},
{
&quot;id&quot;: 10,
&quot;image_couverture&quot;: null,
&quot;nom_categorie&quot;: &quot;tafita-1&quot;,
&quot;descriptions&quot;: &quot;hgjjgj&quot;,
&quot;slug&quot;: &quot;tafita1&quot;,
&quot;created_at&quot;: &quot;2023-03-06T18:15:23.355740Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: 9
},
{
&quot;id&quot;: 11,
&quot;image_couverture&quot;: null,
&quot;nom_categorie&quot;: &quot;Tech&quot;,
&quot;descriptions&quot;: &quot;gd hgjkjg th&quot;,
&quot;slug&quot;: &quot;tech&quot;,
&quot;created_at&quot;: &quot;2023-03-06T18:25:00.949297Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: null
},
{
&quot;id&quot;: 14,
&quot;image_couverture&quot;: null,
&quot;nom_categorie&quot;: &quot;Hotel34&quot;,
&quot;descriptions&quot;: &quot;DJGJ JGKJ&quot;,
&quot;slug&quot;: &quot;hotel34&quot;,
&quot;created_at&quot;: &quot;2023-03-07T16:57:03.666177Z&quot;,
&quot;updated_at&quot;: null,
&quot;parent&quot;: null
}
]

huangapple
  • 本文由 发表于 2023年3月9日 21:01:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75684989.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定