根据数值进行列表嵌套。

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

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:

[[&#39;TopValue1&#39;, 3453, 0],[&#39;TopValue2&#39;, 53453, 0],[&#39;LowerValue1&#39;, &#39;&#39;, 1],[&#39;EvenLowerValue1&#39;, 34534, 2],[&#39;EvenLowerValue2&#39;, 6343, 2],[&#39;LowerValue2&#39;, &#39;&#39;, 1],[&#39;EvenLowerValue1&#39;, &#39;&#39;, 2],[&#39;CanEvenGoLower1&#39;, 42365, 3],[&#39;TopValue3&#39;, 3463, 0]]

For readability:

[
    [&#39;TopValue1&#39;, 3453, 0],
    [&#39;TopValue2&#39;, &#39;&#39;, 0],
        [&#39;LowerValue1&#39;, &#39;&#39;, 1],
            [&#39;EvenLowerValue1&#39;, 34534, 2],
            [&#39;EvenLowerValue2&#39;, 6343, 2],
        [&#39;LowerValue2&#39;, &#39;&#39;, 1],
            [&#39;EvenLowerValue1&#39;, &#39;&#39;, 2],
                [&#39;CanEvenGoLower1&#39;, 42365, 3],
    [&#39;TopValue3&#39;, 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.

[[&#39;TopValue1&#39;, 3453],[&#39;TopValue2&#39;, [[&#39;LowerValue1&#39;, [[&#39;EvenLowerValue1&#39;, 34534],[&#39;EvenLowerValue2&#39;, 6343]]],[&#39;LowerValue2&#39;, [&#39;EvenLowerValue1&#39;, [&#39;CanEvenGoLower1&#39;, 42365]]]]],[&#39;TopValue3&#39;, 3463]]

For readability:

[
    [&#39;TopValue1&#39;, 3453],
    [&#39;TopValue2&#39;, 
        [[&#39;LowerValue1&#39;, 
            [[&#39;EvenLowerValue1&#39;, 34534],
            [&#39;EvenLowerValue2&#39;, 6343]]],
        [&#39;LowerValue2&#39;, 
            [&#39;EvenLowerValue1&#39;, 
                [&#39;CanEvenGoLower1&#39;, 42365]]]]],
    [&#39;TopValue3&#39;, 3463]
]

I tried this atrocity but it's getting me nowhere

parsedData = [[&#39;TopValue1&#39;, 3453, 0],[&#39;TopValue2&#39;, 53453, 0],[&#39;LowerValue1&#39;, &#39;&#39;, 1],[&#39;EvenLowerValue1&#39;, 34534, 2],[&#39;EvenLowerValue2&#39;, 6343, 2],[&#39;LowerValue2&#39;, &#39;&#39;, 1],[&#39;EvenLowerValue1&#39;, &#39;&#39;, 2],[&#39;CanEvenGoLower1&#39;, 42365, 3],[&#39;TopValue3&#39;, 3463, 0]]
for index, i in enumerate(parsedData):
    if index &lt; 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] &lt;= 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] &gt; 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 = [[&quot;&quot;, 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

huangapple
  • 本文由 发表于 2023年8月10日 20:46:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76875867.html
匿名

发表评论

匿名网友

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

确定