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

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

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

问题

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

这是代码:

  1. @api_view(['GET'])
  2. def AllCategoriesList(request):
  3. categories = Categories.objects.filter(parent=None).prefetch_related(
  4. Prefetch('children', queryset=Categories.objects.all(), to_attr='subcategories')
  5. )
  6. def flatten_list(lst):
  7. flat_lst = []
  8. for item in lst:
  9. if isinstance(item, list):
  10. flat_lst.extend(flatten_list(item))
  11. else:
  12. flat_lst.append(item)
  13. return flat_lst
  14. category_list = []
  15. for category in categories:
  16. category_list.append(category)
  17. category_list.extend(category.children.all())
  18. flattened_list = flatten_list(category_list)
  19. serializer = CategorieSerializer(flattened_list, many=True)
  20. return Response(serializer.data)

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

这是输出:

  1. { "id": 1,
  2. "image_couverture": null,
  3. "children": [{ "id": 2,
  4. "image_couverture": null,
  5. "children": [],
  6. "nom_categorie": "nouveau categorie",
  7. "descriptions": "categorie fille",
  8. "slug": "nouveau-categorie",
  9. "created_at": "2023-03-04T08:08:35.667959Z",
  10. "updated_at": null,
  11. "parent": 1
  12. }
  13. ],
  14. "nom_categorie": "Test categorie",
  15. "descriptions": "test categorie",
  16. "slug": "test-categorie",
  17. "created_at": "2023-03-04T06:43:42.628255Z",
  18. "updated_at": null,
  19. "parent": null
  20. },
  21. {
  22. "id": 2,
  23. "image_couverture": null,
  24. "children": [],
  25. "nom_categorie": "nouveau categorie",
  26. "descriptions": "categorie fille",
  27. "slug": "nouveau-categorie",
  28. "created_at": "2023-03-04T08:08:35.667959Z",
  29. "updated_at": null,
  30. "parent": 1
  31. },
  32. {
  33. "id": 3,
  34. "image_couverture": null,
  35. "children": [
  36. {
  37. "id": 4,
  38. "image_couverture": null,
  39. "children": [],
  40. "nom_categorie": "Boissons",
  41. "descriptions": "Test description",
  42. "slug": "boissons",
  43. "created_at": "2023-03-06T10:13:39.229660Z",
  44. "updated_at": null,
  45. "parent": 3
  46. }
  47. ],
  48. "nom_categorie": "Alimentation & vins",
  49. "descriptions": "Test Description",
  50. "slug": "alimentation-vins",
  51. "created_at": "2023-03-06T10:06:24.492310Z",
  52. "updated_at": null,
  53. "parent": null
  54. },
  55. {
  56. "id": 4,
  57. "image_couverture": null,
  58. "children": [],
  59. "nom_categorie": "Boissons",
  60. "descriptions": "Test description",
  61. "slug": "boissons",
  62. "created_at": "2023-03-06T10:13:39.229660Z",
  63. "updated_at": null,
  64. "parent": 3
  65. },
  66. {
  67. "id": 5,
  68. "image_couverture": null,
  69. "children": [
  70. {
  71. "id": 13,
  72. "image_couverture": null,
  73. "children": [],
  74. "nom_categorie": "spa",
  75. "descriptions": "hotel bla",
  76. "slug": "spa",
  77. "created_at": "2023-03-07T16:54:15.145494Z",
  78. "updated_at": null,
  79. "parent": 5
  80. }
  81. ],
  82. "nom_categorie": "VOYAGES et LOCATIONS",
  83. "descriptions": "Test",
  84. "slug": "voyages-et-locations",
  85. "created_at": "2023-03-06T10:58:44.996614Z",
  86. "updated_at": null,
  87. "parent": null
  88. },
  89. {
  90. "id": 13,
  91. "image_couverture": null,
  92. "children": [],
  93. "nom_categorie": "spa",
  94. "descriptions": "hotel bla",
  95. "slug": "spa",
  96. "created_at": "2023-03-07T16:54:15.145494Z",
  97. "updated_at": null,
  98. "parent": 5
  99. },
  100. {
  101. "id": 6,
  102. "image_couverture": null,
  103. "children": [
  104. {
  105. "id": 12,
  106. "image_couverture": null,
  107. "children": [],
  108. "nom_categorie": "Ballon",
  109. "descriptions": "gfjfm jtukj gfdf",
  110. "slug": "ballon",
  111. "created_at": "2023-03-06T18:28:25.832572Z",
  112. "updated_at": null,
  113. "parent": 6
  114. }
  115. ],
  116. "nom_categorie": "SPORTS et FITNESS",
  117. "descriptions": "Test",
  118. "slug": "sports-et-fitness",
  119. "created_at": "2023-03-06T10:59:26.929241Z",
  120. "updated_at": null,
  121. "parent": null
  122. },
  123. {
  124. "id": 12,
  125. "image_couverture": null,
  126. "children": [],
  127. "nom_categorie": "Ballon",
  128. "descriptions": "gfjfm jtukj gfdf",
  129. "slug": "ballon",
  130. "created_at": "2023-03-06T18:28:25.832572Z",
  131. "updated_at": null,
  132. "parent": 6
  133. },
  134. {
  135. "id": 7,
  136. "image_couverture": null,
  137. "children": [
  138. {
  139. "id": 8,
  140. "image_couverture": null,
  141. "children": [],
  142. "nom_categorie": "Chaussures femme",
  143. "descriptions": "Test",
  144. "slug": "chaussures-femme",
  145. "created_at": "2023-03-06T11:15:08.421713Z",
  146. "updated_at": null,
  147. "parent": 7
  148. }
  149. ],
  150. "nom_categorie": "MODE FEMME",
  151. "descriptions": "Test",
  152. "slug": "mode-femme",
  153. "created_at": "2023-03-06T11:13:09.847437Z",
  154. "updated_at": null,
  155. "parent": null
  156. },
  157. {
  158. "id": 8,
  159. "image_couverture": null,
  160. <details>
  161. <summary>英文:</summary>
  162. 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?
  163. This is the code:
  164. ```python
  165. @api_view([&#39;GET&#39;])
  166. def AllCategoriesList(request):
  167. categories = Categories.objects.filter(parent=None).prefetch_related(
  168. Prefetch(&#39;children&#39;, queryset=Categories.objects.all(), to_attr=&#39;subcategories&#39;)
  169. )
  170. def flatten_list(lst):
  171. flat_lst = []
  172. for item in lst:
  173. if isinstance(item, list):
  174. flat_lst.extend(flatten_list(item))
  175. else:
  176. flat_lst.append(item)
  177. return flat_lst
  178. category_list = []
  179. for category in categories:
  180. category_list.append(category)
  181. category_list.extend(category.children.all())
  182. flattened_list = flatten_list(category_list)
  183. serializer = CategorieSerializer(flattened_list, many=True)
  184. 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:

  1. { &quot;id&quot;: 1,
  2. &quot;image_couverture&quot;: null,
  3. &quot;children&quot;: [{ &quot;id&quot;: 2,
  4. &quot;image_couverture&quot;: null,
  5. &quot;children&quot;: [],
  6. &quot;nom_categorie&quot;: &quot;nouveau categorie&quot;,
  7. &quot;descriptions&quot;: &quot;categorie fille&quot;,
  8. &quot;slug&quot;: &quot;nouveau-categorie&quot;,
  9. &quot;created_at&quot;: &quot;2023-03-04T08:08:35.667959Z&quot;,
  10. &quot;updated_at&quot;: null,
  11. &quot;parent&quot;: 1
  12. }
  13. ],
  14. &quot;nom_categorie&quot;: &quot;Test categorie&quot;,
  15. &quot;descriptions&quot;: &quot;test categorie&quot;,
  16. &quot;slug&quot;: &quot;test-categorie&quot;,
  17. &quot;created_at&quot;: &quot;2023-03-04T06:43:42.628255Z&quot;,
  18. &quot;updated_at&quot;: null,
  19. &quot;parent&quot;: null
  20. },
  21. {
  22. &quot;id&quot;: 2,
  23. &quot;image_couverture&quot;: null,
  24. &quot;children&quot;: [],
  25. &quot;nom_categorie&quot;: &quot;nouveau categorie&quot;,
  26. &quot;descriptions&quot;: &quot;categorie fille&quot;,
  27. &quot;slug&quot;: &quot;nouveau-categorie&quot;,
  28. &quot;created_at&quot;: &quot;2023-03-04T08:08:35.667959Z&quot;,
  29. &quot;updated_at&quot;: null,
  30. &quot;parent&quot;: 1
  31. },
  32. {
  33. &quot;id&quot;: 3,
  34. &quot;image_couverture&quot;: null,
  35. &quot;children&quot;: [
  36. {
  37. &quot;id&quot;: 4,
  38. &quot;image_couverture&quot;: null,
  39. &quot;children&quot;: [],
  40. &quot;nom_categorie&quot;: &quot;Boissons&quot;,
  41. &quot;descriptions&quot;: &quot;Test description&quot;,
  42. &quot;slug&quot;: &quot;boissons&quot;,
  43. &quot;created_at&quot;: &quot;2023-03-06T10:13:39.229660Z&quot;,
  44. &quot;updated_at&quot;: null,
  45. &quot;parent&quot;: 3
  46. }
  47. ],
  48. &quot;nom_categorie&quot;: &quot;Alimentation &amp; vins&quot;,
  49. &quot;descriptions&quot;: &quot;Test Description&quot;,
  50. &quot;slug&quot;: &quot;alimentation-vins&quot;,
  51. &quot;created_at&quot;: &quot;2023-03-06T10:06:24.492310Z&quot;,
  52. &quot;updated_at&quot;: null,
  53. &quot;parent&quot;: null
  54. },
  55. {
  56. &quot;id&quot;: 4,
  57. &quot;image_couverture&quot;: null,
  58. &quot;children&quot;: [],
  59. &quot;nom_categorie&quot;: &quot;Boissons&quot;,
  60. &quot;descriptions&quot;: &quot;Test description&quot;,
  61. &quot;slug&quot;: &quot;boissons&quot;,
  62. &quot;created_at&quot;: &quot;2023-03-06T10:13:39.229660Z&quot;,
  63. &quot;updated_at&quot;: null,
  64. &quot;parent&quot;: 3
  65. },
  66. {
  67. &quot;id&quot;: 5,
  68. &quot;image_couverture&quot;: null,
  69. &quot;children&quot;: [
  70. {
  71. &quot;id&quot;: 13,
  72. &quot;image_couverture&quot;: null,
  73. &quot;children&quot;: [],
  74. &quot;nom_categorie&quot;: &quot;spa&quot;,
  75. &quot;descriptions&quot;: &quot;hotel bla&quot;,
  76. &quot;slug&quot;: &quot;spa&quot;,
  77. &quot;created_at&quot;: &quot;2023-03-07T16:54:15.145494Z&quot;,
  78. &quot;updated_at&quot;: null,
  79. &quot;parent&quot;: 5
  80. }
  81. ],
  82. &quot;nom_categorie&quot;: &quot;VOYAGES et LOCATIONS&quot;,
  83. &quot;descriptions&quot;: &quot;Test&quot;,
  84. &quot;slug&quot;: &quot;voyages-et-locations&quot;,
  85. &quot;created_at&quot;: &quot;2023-03-06T10:58:44.996614Z&quot;,
  86. &quot;updated_at&quot;: null,
  87. &quot;parent&quot;: null
  88. },
  89. {
  90. &quot;id&quot;: 13,
  91. &quot;image_couverture&quot;: null,
  92. &quot;children&quot;: [],
  93. &quot;nom_categorie&quot;: &quot;spa&quot;,
  94. &quot;descriptions&quot;: &quot;hotel bla&quot;,
  95. &quot;slug&quot;: &quot;spa&quot;,
  96. &quot;created_at&quot;: &quot;2023-03-07T16:54:15.145494Z&quot;,
  97. &quot;updated_at&quot;: null,
  98. &quot;parent&quot;: 5
  99. },
  100. {
  101. &quot;id&quot;: 6,
  102. &quot;image_couverture&quot;: null,
  103. &quot;children&quot;: [
  104. {
  105. &quot;id&quot;: 12,
  106. &quot;image_couverture&quot;: null,
  107. &quot;children&quot;: [],
  108. &quot;nom_categorie&quot;: &quot;Ballon&quot;,
  109. &quot;descriptions&quot;: &quot;gfjfm jtukj gfdf&quot;,
  110. &quot;slug&quot;: &quot;ballon&quot;,
  111. &quot;created_at&quot;: &quot;2023-03-06T18:28:25.832572Z&quot;,
  112. &quot;updated_at&quot;: null,
  113. &quot;parent&quot;: 6
  114. }
  115. ],
  116. &quot;nom_categorie&quot;: &quot;SPORTS et FITNESS&quot;,
  117. &quot;descriptions&quot;: &quot;Test&quot;,
  118. &quot;slug&quot;: &quot;sports-et-fitness&quot;,
  119. &quot;created_at&quot;: &quot;2023-03-06T10:59:26.929241Z&quot;,
  120. &quot;updated_at&quot;: null,
  121. &quot;parent&quot;: null
  122. },
  123. {
  124. &quot;id&quot;: 12,
  125. &quot;image_couverture&quot;: null,
  126. &quot;children&quot;: [],
  127. &quot;nom_categorie&quot;: &quot;Ballon&quot;,
  128. &quot;descriptions&quot;: &quot;gfjfm jtukj gfdf&quot;,
  129. &quot;slug&quot;: &quot;ballon&quot;,
  130. &quot;created_at&quot;: &quot;2023-03-06T18:28:25.832572Z&quot;,
  131. &quot;updated_at&quot;: null,
  132. &quot;parent&quot;: 6
  133. },
  134. {
  135. &quot;id&quot;: 7,
  136. &quot;image_couverture&quot;: null,
  137. &quot;children&quot;: [
  138. {
  139. &quot;id&quot;: 8,
  140. &quot;image_couverture&quot;: null,
  141. &quot;children&quot;: [],
  142. &quot;nom_categorie&quot;: &quot;Chaussures femme&quot;,
  143. &quot;descriptions&quot;: &quot;Test&quot;,
  144. &quot;slug&quot;: &quot;chaussures-femme&quot;,
  145. &quot;created_at&quot;: &quot;2023-03-06T11:15:08.421713Z&quot;,
  146. &quot;updated_at&quot;: null,
  147. &quot;parent&quot;: 7
  148. }
  149. ],
  150. &quot;nom_categorie&quot;: &quot;MODE FEMME&quot;,
  151. &quot;descriptions&quot;: &quot;Test&quot;,
  152. &quot;slug&quot;: &quot;mode-femme&quot;,
  153. &quot;created_at&quot;: &quot;2023-03-06T11:13:09.847437Z&quot;,
  154. &quot;updated_at&quot;: null,
  155. &quot;parent&quot;: null
  156. },
  157. {
  158. &quot;id&quot;: 8,
  159. &quot;image_couverture&quot;: null,
  160. &quot;children&quot;: [],
  161. &quot;nom_categorie&quot;: &quot;Chaussures femme&quot;,
  162. &quot;descriptions&quot;: &quot;Test&quot;,
  163. &quot;slug&quot;: &quot;chaussures-femme&quot;,
  164. &quot;created_at&quot;: &quot;2023-03-06T11:15:08.421713Z&quot;,
  165. &quot;updated_at&quot;: null,
  166. &quot;parent&quot;: 7
  167. },
  168. {
  169. &quot;id&quot;: 9,
  170. &quot;image_couverture&quot;: null,
  171. &quot;children&quot;: [
  172. {
  173. &quot;id&quot;: 10,
  174. &quot;image_couverture&quot;: null,
  175. &quot;children&quot;: [],
  176. &quot;nom_categorie&quot;: &quot;tafita-1&quot;,
  177. &quot;descriptions&quot;: &quot;hgjjgj&quot;,
  178. &quot;slug&quot;: &quot;tafita1&quot;,
  179. &quot;created_at&quot;: &quot;2023-03-06T18:15:23.355740Z&quot;,
  180. &quot;updated_at&quot;: null,
  181. &quot;parent&quot;: 9
  182. }
  183. ],
  184. &quot;nom_categorie&quot;: &quot;tafita&quot;,
  185. &quot;descriptions&quot;: &quot;nljbk&quot;,
  186. &quot;slug&quot;: &quot;tafita&quot;,
  187. &quot;created_at&quot;: &quot;2023-03-06T18:14:58.259709Z&quot;,
  188. &quot;updated_at&quot;: null,
  189. &quot;parent&quot;: null
  190. },
  191. {
  192. &quot;id&quot;: 10,
  193. &quot;image_couverture&quot;: null,
  194. &quot;children&quot;: [],
  195. &quot;nom_categorie&quot;: &quot;tafita-1&quot;,
  196. &quot;descriptions&quot;: &quot;hgjjgj&quot;,
  197. &quot;slug&quot;: &quot;tafita1&quot;,
  198. &quot;created_at&quot;: &quot;2023-03-06T18:15:23.355740Z&quot;,
  199. &quot;updated_at&quot;: null,
  200. &quot;parent&quot;: 9
  201. },
  202. {
  203. &quot;id&quot;: 11,
  204. &quot;image_couverture&quot;: null,
  205. &quot;children&quot;: [],
  206. &quot;nom_categorie&quot;: &quot;Tech&quot;,
  207. &quot;descriptions&quot;: &quot;gd hgjkjg th&quot;,
  208. &quot;slug&quot;: &quot;tech&quot;,
  209. &quot;created_at&quot;: &quot;2023-03-06T18:25:00.949297Z&quot;,
  210. &quot;updated_at&quot;: null,
  211. &quot;parent&quot;: null
  212. },
  213. {
  214. &quot;id&quot;: 14,
  215. &quot;image_couverture&quot;: null,
  216. &quot;children&quot;: [],
  217. &quot;nom_categorie&quot;: &quot;Hotel34&quot;,
  218. &quot;descriptions&quot;: &quot;DJGJ JGKJ&quot;,
  219. &quot;slug&quot;: &quot;hotel34&quot;,
  220. &quot;created_at&quot;: &quot;2023-03-07T16:57:03.666177Z&quot;,
  221. &quot;updated_at&quot;: null,
  222. &quot;parent&quot;: null
  223. }
  224. ]

答案1

得分: 0

以下是翻译的代码部分:

  1. def flatten_list(data):
  2. flat_lst = []
  3. if isinstance(data, list):
  4. for item in data:
  5. flat_lst.append(item)
  6. flat_lst.extend(flatten_list(item))
  7. return flat_lst
  8. if isinstance(data, dict):
  9. ## ----------------------
  10. ## 使用项目的副本,以便我们不直接
  11. ## 迭代字典
  12. ## ----------------------
  13. for key, value in list(data.items()):
  14. flat_lst.extend(flatten_list(value))
  15. ### ----------------------
  16. ### 你不想要 "children": []
  17. ### ----------------------
  18. if isinstance(value, list):
  19. del data[key]
  20. ### ----------------------
  21. ## ----------------------
  22. return flat_lst
  23. data = json.loads(data_raw)
  24. data = flatten_list(data)
  25. ## ---------------
  26. ## 仅唯一的项目(按id)
  27. ## ---------------
  28. data = list({item["id"]: item for item in data}.values())
  29. ## ---------------
  30. 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:

  1. data_raw = &quot;&quot;&quot;
  2. [
  3. {
  4. &quot;id&quot;: 1,
  5. &quot;image_couverture&quot;: null,
  6. &quot;children&quot;: [
  7. {
  8. &quot;id&quot;: 2,
  9. &quot;image_couverture&quot;: null,
  10. &quot;children&quot;: [],
  11. &quot;nom_categorie&quot;: &quot;nouveau categorie&quot;,
  12. &quot;descriptions&quot;: &quot;categorie fille&quot;,
  13. &quot;slug&quot;: &quot;nouveau-categorie&quot;,
  14. &quot;created_at&quot;: &quot;2023-03-04T08:08:35.667959Z&quot;,
  15. &quot;updated_at&quot;: null,
  16. &quot;parent&quot;: 1
  17. }
  18. ],
  19. &quot;nom_categorie&quot;: &quot;Test categorie&quot;,
  20. &quot;descriptions&quot;: &quot;test categorie&quot;,
  21. &quot;slug&quot;: &quot;test-categorie&quot;,
  22. &quot;created_at&quot;: &quot;2023-03-04T06:43:42.628255Z&quot;,
  23. &quot;updated_at&quot;: null,
  24. &quot;parent&quot;: null
  25. },
  26. {
  27. &quot;id&quot;: 2,
  28. &quot;image_couverture&quot;: null,
  29. &quot;children&quot;: [],
  30. &quot;nom_categorie&quot;: &quot;nouveau categorie&quot;,
  31. &quot;descriptions&quot;: &quot;categorie fille&quot;,
  32. &quot;slug&quot;: &quot;nouveau-categorie&quot;,
  33. &quot;created_at&quot;: &quot;2023-03-04T08:08:35.667959Z&quot;,
  34. &quot;updated_at&quot;: null,
  35. &quot;parent&quot;: 1
  36. },
  37. {
  38. &quot;id&quot;: 3,
  39. &quot;image_couverture&quot;: null,
  40. &quot;children&quot;: [
  41. {
  42. &quot;id&quot;: 4,
  43. &quot;image_couverture&quot;: null,
  44. &quot;children&quot;: [],
  45. &quot;nom_categorie&quot;: &quot;Boissons&quot;,
  46. &quot;descriptions&quot;: &quot;Test description&quot;,
  47. &quot;slug&quot;: &quot;boissons&quot;,
  48. &quot;created_at&quot;: &quot;2023-03-06T10:13:39.229660Z&quot;,
  49. &quot;updated_at&quot;: null,
  50. &quot;parent&quot;: 3
  51. }
  52. ],
  53. &quot;nom_categorie&quot;: &quot;Alimentation &amp; vins&quot;,
  54. &quot;descriptions&quot;: &quot;Test Description&quot;,
  55. &quot;slug&quot;: &quot;alimentation-vins&quot;,
  56. &quot;created_at&quot;: &quot;2023-03-06T10:06:24.492310Z&quot;,
  57. &quot;updated_at&quot;: null,
  58. &quot;parent&quot;: null
  59. },
  60. {
  61. &quot;id&quot;: 4,
  62. &quot;image_couverture&quot;: null,
  63. &quot;children&quot;: [],
  64. &quot;nom_categorie&quot;: &quot;Boissons&quot;,
  65. &quot;descriptions&quot;: &quot;Test description&quot;,
  66. &quot;slug&quot;: &quot;boissons&quot;,
  67. &quot;created_at&quot;: &quot;2023-03-06T10:13:39.229660Z&quot;,
  68. &quot;updated_at&quot;: null,
  69. &quot;parent&quot;: 3
  70. },
  71. {
  72. &quot;id&quot;: 5,
  73. &quot;image_couverture&quot;: null,
  74. &quot;children&quot;: [
  75. {
  76. &quot;id&quot;: 13,
  77. &quot;image_couverture&quot;: null,
  78. &quot;children&quot;: [],
  79. &quot;nom_categorie&quot;: &quot;spa&quot;,
  80. &quot;descriptions&quot;: &quot;hotel bla&quot;,
  81. &quot;slug&quot;: &quot;spa&quot;,
  82. &quot;created_at&quot;: &quot;2023-03-07T16:54:15.145494Z&quot;,
  83. &quot;updated_at&quot;: null,
  84. &quot;parent&quot;: 5
  85. }
  86. ],
  87. &quot;nom_categorie&quot;: &quot;VOYAGES et LOCATIONS&quot;,
  88. &quot;descriptions&quot;: &quot;Test&quot;,
  89. &quot;slug&quot;: &quot;voyages-et-locations&quot;,
  90. &quot;created_at&quot;: &quot;2023-03-06T10:58:44.996614Z&quot;,
  91. &quot;updated_at&quot;: null,
  92. &quot;parent&quot;: null
  93. },
  94. {
  95. &quot;id&quot;: 13,
  96. &quot;image_couverture&quot;: null,
  97. &quot;children&quot;: [],
  98. &quot;nom_categorie&quot;: &quot;spa&quot;,
  99. &quot;descriptions&quot;: &quot;hotel bla&quot;,
  100. &quot;slug&quot;: &quot;spa&quot;,
  101. &quot;created_at&quot;: &quot;2023-03-07T16:54:15.145494Z&quot;,
  102. &quot;updated_at&quot;: null,
  103. &quot;parent&quot;: 5
  104. },
  105. {
  106. &quot;id&quot;: 6,
  107. &quot;image_couverture&quot;: null,
  108. &quot;children&quot;: [
  109. {
  110. &quot;id&quot;: 12,
  111. &quot;image_couverture&quot;: null,
  112. &quot;children&quot;: [],
  113. &quot;nom_categorie&quot;: &quot;Ballon&quot;,
  114. &quot;descriptions&quot;: &quot;gfjfm jtukj gfdf&quot;,
  115. &quot;slug&quot;: &quot;ballon&quot;,
  116. &quot;created_at&quot;: &quot;2023-03-06T18:28:25.832572Z&quot;,
  117. &quot;updated_at&quot;: null,
  118. &quot;parent&quot;: 6
  119. }
  120. ],
  121. &quot;nom_categorie&quot;: &quot;SPORTS et FITNESS&quot;,
  122. &quot;descriptions&quot;: &quot;Test&quot;,
  123. &quot;slug&quot;: &quot;sports-et-fitness&quot;,
  124. &quot;created_at&quot;: &quot;2023-03-06T10:59:26.929241Z&quot;,
  125. &quot;updated_at&quot;: null,
  126. &quot;parent&quot;: null
  127. },
  128. {
  129. &quot;id&quot;: 12,
  130. &quot;image_couverture&quot;: null,
  131. &quot;children&quot;: [],
  132. &quot;nom_categorie&quot;: &quot;Ballon&quot;,
  133. &quot;descriptions&quot;: &quot;gfjfm jtukj gfdf&quot;,
  134. &quot;slug&quot;: &quot;ballon&quot;,
  135. &quot;created_at&quot;: &quot;2023-03-06T18:28:25.832572Z&quot;,
  136. &quot;updated_at&quot;: null,
  137. &quot;parent&quot;: 6
  138. },
  139. {
  140. &quot;id&quot;: 7,
  141. &quot;image_couverture&quot;: null,
  142. &quot;children&quot;: [
  143. {
  144. &quot;id&quot;: 8,
  145. &quot;image_couverture&quot;: null,
  146. &quot;children&quot;: [],
  147. &quot;nom_categorie&quot;: &quot;Chaussures femme&quot;,
  148. &quot;descriptions&quot;: &quot;Test&quot;,
  149. &quot;slug&quot;: &quot;chaussures-femme&quot;,
  150. &quot;created_at&quot;: &quot;2023-03-06T11:15:08.421713Z&quot;,
  151. &quot;updated_at&quot;: null,
  152. &quot;parent&quot;: 7
  153. }
  154. ],
  155. &quot;nom_categorie&quot;: &quot;MODE FEMME&quot;,
  156. &quot;descriptions&quot;: &quot;Test&quot;,
  157. &quot;slug&quot;: &quot;mode-femme&quot;,
  158. &quot;created_at&quot;: &quot;2023-03-06T11:13:09.847437Z&quot;,
  159. &quot;updated_at&quot;: null,
  160. &quot;parent&quot;: null
  161. },
  162. {
  163. &quot;id&quot;: 8,
  164. &quot;image_couverture&quot;: null,
  165. &quot;children&quot;: [],
  166. &quot;nom_categorie&quot;: &quot;Chaussures femme&quot;,
  167. &quot;descriptions&quot;: &quot;Test&quot;,
  168. &quot;slug&quot;: &quot;chaussures-femme&quot;,
  169. &quot;created_at&quot;: &quot;2023-03-06T11:15:08.421713Z&quot;,
  170. &quot;updated_at&quot;: null,
  171. &quot;parent&quot;: 7
  172. },
  173. {
  174. &quot;id&quot;: 9,
  175. &quot;image_couverture&quot;: null,
  176. &quot;children&quot;: [
  177. {
  178. &quot;id&quot;: 10,
  179. &quot;image_couverture&quot;: null,
  180. &quot;children&quot;: [],
  181. &quot;nom_categorie&quot;: &quot;tafita-1&quot;,
  182. &quot;descriptions&quot;: &quot;hgjjgj&quot;,
  183. &quot;slug&quot;: &quot;tafita1&quot;,
  184. &quot;created_at&quot;: &quot;2023-03-06T18:15:23.355740Z&quot;,
  185. &quot;updated_at&quot;: null,
  186. &quot;parent&quot;: 9
  187. }
  188. ],
  189. &quot;nom_categorie&quot;: &quot;tafita&quot;,
  190. &quot;descriptions&quot;: &quot;nljbk&quot;,
  191. &quot;slug&quot;: &quot;tafita&quot;,
  192. &quot;created_at&quot;: &quot;2023-03-06T18:14:58.259709Z&quot;,
  193. &quot;updated_at&quot;: null,
  194. &quot;parent&quot;: null
  195. },
  196. {
  197. &quot;id&quot;: 10,
  198. &quot;image_couverture&quot;: null,
  199. &quot;children&quot;: [],
  200. &quot;nom_categorie&quot;: &quot;tafita-1&quot;,
  201. &quot;descriptions&quot;: &quot;hgjjgj&quot;,
  202. &quot;slug&quot;: &quot;tafita1&quot;,
  203. &quot;created_at&quot;: &quot;2023-03-06T18:15:23.355740Z&quot;,
  204. &quot;updated_at&quot;: null,
  205. &quot;parent&quot;: 9
  206. },
  207. {
  208. &quot;id&quot;: 11,
  209. &quot;image_couverture&quot;: null,
  210. &quot;children&quot;: [],
  211. &quot;nom_categorie&quot;: &quot;Tech&quot;,
  212. &quot;descriptions&quot;: &quot;gd hgjkjg th&quot;,
  213. &quot;slug&quot;: &quot;tech&quot;,
  214. &quot;created_at&quot;: &quot;2023-03-06T18:25:00.949297Z&quot;,
  215. &quot;updated_at&quot;: null,
  216. &quot;parent&quot;: null
  217. },
  218. {
  219. &quot;id&quot;: 14,
  220. &quot;image_couverture&quot;: null,
  221. &quot;children&quot;: [],
  222. &quot;nom_categorie&quot;: &quot;Hotel34&quot;,
  223. &quot;descriptions&quot;: &quot;DJGJ JGKJ&quot;,
  224. &quot;slug&quot;: &quot;hotel34&quot;,
  225. &quot;created_at&quot;: &quot;2023-03-07T16:57:03.666177Z&quot;,
  226. &quot;updated_at&quot;: null,
  227. &quot;parent&quot;: null
  228. }
  229. ]
  230. &quot;&quot;&quot;

Then you can:

  1. def flatten_list(data):
  2. flat_lst = []
  3. if isinstance(data, list):
  4. for item in data:
  5. flat_lst.append(item)
  6. flat_lst.extend(flatten_list(item))
  7. return flat_lst
  8. if isinstance(data, dict):
  9. ## ----------------------
  10. ## Use a copy of the items so we are not directly
  11. ## iterating over the dictionary
  12. ## ----------------------
  13. for key, value in list(data.items()):
  14. flat_lst.extend(flatten_list(value))
  15. ### ----------------------
  16. ### you don&#39;t want children&quot;: []
  17. ### ----------------------
  18. if isinstance(value, list):
  19. del data[key]
  20. ### ----------------------
  21. ## ----------------------
  22. return flat_lst
  23. data = json.loads(data_raw)
  24. data = flatten_list(data)
  25. ## ---------------
  26. ## just the unique items (by id)
  27. ## ---------------
  28. data = list({item[&quot;id&quot;]: item for item in data}.values())
  29. ## ---------------
  30. print(json.dumps(data, indent=4))

That should give you:

  1. [
  2. {
  3. &quot;id&quot;: 1,
  4. &quot;image_couverture&quot;: null,
  5. &quot;nom_categorie&quot;: &quot;Test categorie&quot;,
  6. &quot;descriptions&quot;: &quot;test categorie&quot;,
  7. &quot;slug&quot;: &quot;test-categorie&quot;,
  8. &quot;created_at&quot;: &quot;2023-03-04T06:43:42.628255Z&quot;,
  9. &quot;updated_at&quot;: null,
  10. &quot;parent&quot;: null
  11. },
  12. {
  13. &quot;id&quot;: 2,
  14. &quot;image_couverture&quot;: null,
  15. &quot;nom_categorie&quot;: &quot;nouveau categorie&quot;,
  16. &quot;descriptions&quot;: &quot;categorie fille&quot;,
  17. &quot;slug&quot;: &quot;nouveau-categorie&quot;,
  18. &quot;created_at&quot;: &quot;2023-03-04T08:08:35.667959Z&quot;,
  19. &quot;updated_at&quot;: null,
  20. &quot;parent&quot;: 1
  21. },
  22. {
  23. &quot;id&quot;: 3,
  24. &quot;image_couverture&quot;: null,
  25. &quot;nom_categorie&quot;: &quot;Alimentation &amp; vins&quot;,
  26. &quot;descriptions&quot;: &quot;Test Description&quot;,
  27. &quot;slug&quot;: &quot;alimentation-vins&quot;,
  28. &quot;created_at&quot;: &quot;2023-03-06T10:06:24.492310Z&quot;,
  29. &quot;updated_at&quot;: null,
  30. &quot;parent&quot;: null
  31. },
  32. {
  33. &quot;id&quot;: 4,
  34. &quot;image_couverture&quot;: null,
  35. &quot;nom_categorie&quot;: &quot;Boissons&quot;,
  36. &quot;descriptions&quot;: &quot;Test description&quot;,
  37. &quot;slug&quot;: &quot;boissons&quot;,
  38. &quot;created_at&quot;: &quot;2023-03-06T10:13:39.229660Z&quot;,
  39. &quot;updated_at&quot;: null,
  40. &quot;parent&quot;: 3
  41. },
  42. {
  43. &quot;id&quot;: 5,
  44. &quot;image_couverture&quot;: null,
  45. &quot;nom_categorie&quot;: &quot;VOYAGES et LOCATIONS&quot;,
  46. &quot;descriptions&quot;: &quot;Test&quot;,
  47. &quot;slug&quot;: &quot;voyages-et-locations&quot;,
  48. &quot;created_at&quot;: &quot;2023-03-06T10:58:44.996614Z&quot;,
  49. &quot;updated_at&quot;: null,
  50. &quot;parent&quot;: null
  51. },
  52. {
  53. &quot;id&quot;: 13,
  54. &quot;image_couverture&quot;: null,
  55. &quot;nom_categorie&quot;: &quot;spa&quot;,
  56. &quot;descriptions&quot;: &quot;hotel bla&quot;,
  57. &quot;slug&quot;: &quot;spa&quot;,
  58. &quot;created_at&quot;: &quot;2023-03-07T16:54:15.145494Z&quot;,
  59. &quot;updated_at&quot;: null,
  60. &quot;parent&quot;: 5
  61. },
  62. {
  63. &quot;id&quot;: 6,
  64. &quot;image_couverture&quot;: null,
  65. &quot;nom_categorie&quot;: &quot;SPORTS et FITNESS&quot;,
  66. &quot;descriptions&quot;: &quot;Test&quot;,
  67. &quot;slug&quot;: &quot;sports-et-fitness&quot;,
  68. &quot;created_at&quot;: &quot;2023-03-06T10:59:26.929241Z&quot;,
  69. &quot;updated_at&quot;: null,
  70. &quot;parent&quot;: null
  71. },
  72. {
  73. &quot;id&quot;: 12,
  74. &quot;image_couverture&quot;: null,
  75. &quot;nom_categorie&quot;: &quot;Ballon&quot;,
  76. &quot;descriptions&quot;: &quot;gfjfm jtukj gfdf&quot;,
  77. &quot;slug&quot;: &quot;ballon&quot;,
  78. &quot;created_at&quot;: &quot;2023-03-06T18:28:25.832572Z&quot;,
  79. &quot;updated_at&quot;: null,
  80. &quot;parent&quot;: 6
  81. },
  82. {
  83. &quot;id&quot;: 7,
  84. &quot;image_couverture&quot;: null,
  85. &quot;nom_categorie&quot;: &quot;MODE FEMME&quot;,
  86. &quot;descriptions&quot;: &quot;Test&quot;,
  87. &quot;slug&quot;: &quot;mode-femme&quot;,
  88. &quot;created_at&quot;: &quot;2023-03-06T11:13:09.847437Z&quot;,
  89. &quot;updated_at&quot;: null,
  90. &quot;parent&quot;: null
  91. },
  92. {
  93. &quot;id&quot;: 8,
  94. &quot;image_couverture&quot;: null,
  95. &quot;nom_categorie&quot;: &quot;Chaussures femme&quot;,
  96. &quot;descriptions&quot;: &quot;Test&quot;,
  97. &quot;slug&quot;: &quot;chaussures-femme&quot;,
  98. &quot;created_at&quot;: &quot;2023-03-06T11:15:08.421713Z&quot;,
  99. &quot;updated_at&quot;: null,
  100. &quot;parent&quot;: 7
  101. },
  102. {
  103. &quot;id&quot;: 9,
  104. &quot;image_couverture&quot;: null,
  105. &quot;nom_categorie&quot;: &quot;tafita&quot;,
  106. &quot;descriptions&quot;: &quot;nljbk&quot;,
  107. &quot;slug&quot;: &quot;tafita&quot;,
  108. &quot;created_at&quot;: &quot;2023-03-06T18:14:58.259709Z&quot;,
  109. &quot;updated_at&quot;: null,
  110. &quot;parent&quot;: null
  111. },
  112. {
  113. &quot;id&quot;: 10,
  114. &quot;image_couverture&quot;: null,
  115. &quot;nom_categorie&quot;: &quot;tafita-1&quot;,
  116. &quot;descriptions&quot;: &quot;hgjjgj&quot;,
  117. &quot;slug&quot;: &quot;tafita1&quot;,
  118. &quot;created_at&quot;: &quot;2023-03-06T18:15:23.355740Z&quot;,
  119. &quot;updated_at&quot;: null,
  120. &quot;parent&quot;: 9
  121. },
  122. {
  123. &quot;id&quot;: 11,
  124. &quot;image_couverture&quot;: null,
  125. &quot;nom_categorie&quot;: &quot;Tech&quot;,
  126. &quot;descriptions&quot;: &quot;gd hgjkjg th&quot;,
  127. &quot;slug&quot;: &quot;tech&quot;,
  128. &quot;created_at&quot;: &quot;2023-03-06T18:25:00.949297Z&quot;,
  129. &quot;updated_at&quot;: null,
  130. &quot;parent&quot;: null
  131. },
  132. {
  133. &quot;id&quot;: 14,
  134. &quot;image_couverture&quot;: null,
  135. &quot;nom_categorie&quot;: &quot;Hotel34&quot;,
  136. &quot;descriptions&quot;: &quot;DJGJ JGKJ&quot;,
  137. &quot;slug&quot;: &quot;hotel34&quot;,
  138. &quot;created_at&quot;: &quot;2023-03-07T16:57:03.666177Z&quot;,
  139. &quot;updated_at&quot;: null,
  140. &quot;parent&quot;: null
  141. }
  142. ]

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:

确定