[英]Python recursion: nested list from flat list
I need figuring out how to create (what I think needs to be) a recursive function. 我需要弄清楚如何创建(我认为需要)递归函数。 My brain has never coped well with recursion.
我的大脑从来没有很好地应对过递归。
I have a flat collection of items, that needs to be turned into a nested collection based on a value in each item. 我有一个扁平的项目集合,需要根据每个项目中的值将其转换为一个嵌套集合。 Each item has, among other attributes, a type.
除其他属性外,每个项目都有一个类型。 One possible item.type is "group header".
一种可能的item.type是“组头”。 Each "group header" will be closed by a "group footer".
每个“组页眉”将由“组页脚”关闭。 I need to nest the collection based on those two types.
我需要基于这两种类型嵌套该集合。
The collection might look like this: 该集合可能如下所示:
I want to make that collection look more like this: 我想使该集合看起来像这样:
There can be any depth of nesting, hence (I think) the need for recursion. 可以有任何深度的嵌套,因此(我认为)需要递归。
Any pointers on how to do it greatly appreciated. 任何关于如何做到这一点的指针都非常感谢。 I simply cannot get my head around it, and I can't find an example online where a tag (in my case, "group footer") is used to jump back up a nest level.
我根本无法解决问题,也找不到在线示例,其中使用标签(在我的情况下为“组页脚”)跳回到嵌套级别。
Here are the beginnings of a python fiddle to work with: http://pythonfiddle.com/recursion-fiddle-ninety-nine 这是可以使用的python小提琴的开头: http : //pythonfiddle.com/recursion-fiddle-ninety-nine
Example data from link: 来自链接的示例数据:
test_data = [{"id":1, "type":"blurb", "info":"This is the blurb"},
{"id":2, "type":"header", "info":"This is the first group header"},
{"id":3, "type":"question", "info":"This is the first question"},
{"id":4, "type":"question", "info":"This is the second question"},
{"id":5, "type":"header", "info":"This is the second group header"},
{"id":6, "type":"question", "info":"This is the third question"},
{"id":7, "type":"question", "info":"This is the fourth question"},
{"id":8, "type":"footer", "info":"This is the footer for the second header"},
{"id":9, "type":"question", "info":"This is the fifth question"},
{"id":10, "type":"footer", "info":"This is the footer for the first header"}]
thanks in advance 提前致谢
Jay 周杰伦
I don't know how exactly you want the resulting list to be formatted, but here you go: 我不知道您要如何精确格式化结果列表,但是您可以在这里进行:
nested_data = []
stack= []
for item in test_data:
if item['type']=='header': # if it's a header
# add [item] to the list (the footer will be appended to this later)
header= [[item]]
nested_data.append(header)
# push this list onto the stack so we can continue appending to it
# after we've found the footer
stack.append(nested_data)
nested_data= header
elif item['type']=='footer':
# if it's a footer, pop the last list off the stack
nested_data= stack.pop(-1)
# and append the footer after the header so that
# [header, footer] is the first item
nested_data[-1][0].append(item)
else:
# if it's just a boring ol' item, all we need do is append it
nested_data.append(item)
This produces (the nested_data
variable holds the result): 这将产生(
nested_data
变量保存结果):
[
{
"info": "This is the blurb",
"type": "blurb",
"id": 1
},
[
[
{
"info": "This is the first group header",
"type": "header",
"id": 2
},
{
"info": "This is the footer for the first header",
"type": "footer",
"id": 10
}
],
{
"info": "This is the first question",
"type": "question",
"id": 3
},
{
"info": "This is the second question",
"type": "question",
"id": 4
},
[
[
{
"info": "This is the second group header",
"type": "header",
"id": 5
},
{
"info": "This is the footer for the second header",
"type": "footer",
"id": 8
}
],
{
"info": "This is the third question",
"type": "question",
"id": 6
},
{
"info": "This is the fourth question",
"type": "question",
"id": 7
}
],
{
"info": "This is the fifth question",
"type": "question",
"id": 9
}
]
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.