英文:
Nesting lists based on the value
问题
我有一个包含一些名称、值和表示嵌套深度的整数的列表列表。理论上,嵌套可以无限进行,但实际上最多有5个级别。看起来像这样:
[
['TopValue1', 3453, 0],
['TopValue2', 53453, 0],
['LowerValue1', '', 1],
['EvenLowerValue1', 34534, 2],
['EvenLowerValue2', 6343, 2],
['LowerValue2', '', 1],
['EvenLowerValue1', '', 2],
['CanEvenGoLower1', 42365, 3],
['TopValue3', 3463, 0]
]
是否有一种方法可以对其进行排序,以提供这样的结构?顶级元素中缺少值是有意的,它们将被来自较低级别的列表替换。
[
['TopValue1', 3453],
['TopValue2', [
['LowerValue1', [
['EvenLowerValue1', 34534],
['EvenLowerValue2', 6343]
]],
['LowerValue2', [
['EvenLowerValue1', [
['CanEvenGoLower1', 42365]
]]
]]
]],
['TopValue3', 3463]
]
我尝试了下面这段代码,但没有取得进展:
parsedData = [['TopValue1', 3453, 0],['TopValue2', 53453, 0],['LowerValue1', '', 1],['EvenLowerValue1', 34534, 2],['EvenLowerValue2', 6343, 2],['LowerValue2', '', 1],['EvenLowerValue1', '', 2],['CanEvenGoLower1', 42365, 3],['TopValue3', 3463, 0]]
for index, i in enumerate(parsedData):
if index < len(parsedData)-1:
if i[2] == 0 and index == 0: #case of first item
collapsedList.append(i)
collapsedList[index].pop(2)
elif i[2] == 0 and parsedData[index+1][2] == 0:
collapsedList.append(i)
collapsedList[index].pop(2)
elif i[2] <= parsedData[index+1][2]:
collapsedList.append(i)
print(collapsedList)
if i[2] == parsedData[index+1][2]:
collapsedList[index-1].insert(1,i)
elif i[2] > parsedData[index+1][2]:
collapsedList[index-1].insert(1,i)
else:
pass
希望这能帮助你在代码中找到正确的结构。
英文:
I have a list of lists that include some name, values and integer indicating depth of nesting. Nesting can theoretically go infinitely, Realistically 5 levels. Looks something like this:
[['TopValue1', 3453, 0],['TopValue2', 53453, 0],['LowerValue1', '', 1],['EvenLowerValue1', 34534, 2],['EvenLowerValue2', 6343, 2],['LowerValue2', '', 1],['EvenLowerValue1', '', 2],['CanEvenGoLower1', 42365, 3],['TopValue3', 3463, 0]]
For readability:
[
['TopValue1', 3453, 0],
['TopValue2', '', 0],
['LowerValue1', '', 1],
['EvenLowerValue1', 34534, 2],
['EvenLowerValue2', 6343, 2],
['LowerValue2', '', 1],
['EvenLowerValue1', '', 2],
['CanEvenGoLower1', 42365, 3],
['TopValue3', 3463, 0]
]
Is there a way to sort it in the way that would provide structure like this? Lack of values in 'top' elements is intentional, they are getting replaced by lists from 'lower' levels.
[['TopValue1', 3453],['TopValue2', [['LowerValue1', [['EvenLowerValue1', 34534],['EvenLowerValue2', 6343]]],['LowerValue2', ['EvenLowerValue1', ['CanEvenGoLower1', 42365]]]]],['TopValue3', 3463]]
For readability:
[
['TopValue1', 3453],
['TopValue2',
[['LowerValue1',
[['EvenLowerValue1', 34534],
['EvenLowerValue2', 6343]]],
['LowerValue2',
['EvenLowerValue1',
['CanEvenGoLower1', 42365]]]]],
['TopValue3', 3463]
]
I tried this atrocity but it's getting me nowhere
parsedData = [['TopValue1', 3453, 0],['TopValue2', 53453, 0],['LowerValue1', '', 1],['EvenLowerValue1', 34534, 2],['EvenLowerValue2', 6343, 2],['LowerValue2', '', 1],['EvenLowerValue1', '', 2],['CanEvenGoLower1', 42365, 3],['TopValue3', 3463, 0]]
for index, i in enumerate(parsedData):
if index < len(parsedData)-1:
if i[2] == 0 and index == 0: #case of first item
collapsedList.append(i)
collapsedList[index].pop(2)
elif i[2] == 0 and parsedData[index+1][2] == 0:
collapsedList.append(i)
collapsedList[index].pop(2)
elif i[2] <= parsedData[index+1][2]:
collapsedList.append(i)
print(collapsedList)
if i[2] == parsedData[index+1][2]:
collapsedList[index-1].insert(1,i)
elif i[2] > parsedData[index+1][2]:
collapsedList[index-1].insert(1,i)
else:
pass
答案1
得分: 1
def make_hierarchy(parsedData):
root = []
stack = [["", root]]
for value1, value2, level in parsedData:
stack[level+1:] = [] # 剪裁堆栈
if type(stack[-1][1]) is not list:
stack[-1][1] = [] # 用新列表替换第二个成员
stack[-1][1].append([value1, value2]) # 插入对
stack.append(stack[-1][1][-1]) # ...同时将此对堆叠
return root
英文:
The idea is to use a stack. If the level decreases, then clip the stack to correspond to that level, and when the level increases (with 1), then append a pair to that stack. Some extra logic is needed to replace the second member of a pair with a new list if it is not a leaf node:
def make_hierarchy(parsedData):
root = []
stack = [["", root]]
for value1, value2, level in parsedData:
stack[level+1:] = [] # clip the stack
if type(stack[-1][1]) is not list:
stack[-1][1] = [] # replace second value with new list
stack[-1][1].append([value1, value2]) # inject pair
stack.append(stack[-1][1][-1]) # ...and also stack this pair
return root
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论