根据数值进行列表嵌套。

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

Nesting lists based on the value

问题

我有一个包含一些名称、值和表示嵌套深度的整数的列表列表。理论上,嵌套可以无限进行,但实际上最多有5个级别。看起来像这样:

  1. [
  2. ['TopValue1', 3453, 0],
  3. ['TopValue2', 53453, 0],
  4. ['LowerValue1', '', 1],
  5. ['EvenLowerValue1', 34534, 2],
  6. ['EvenLowerValue2', 6343, 2],
  7. ['LowerValue2', '', 1],
  8. ['EvenLowerValue1', '', 2],
  9. ['CanEvenGoLower1', 42365, 3],
  10. ['TopValue3', 3463, 0]
  11. ]

是否有一种方法可以对其进行排序,以提供这样的结构?顶级元素中缺少值是有意的,它们将被来自较低级别的列表替换。

  1. [
  2. ['TopValue1', 3453],
  3. ['TopValue2', [
  4. ['LowerValue1', [
  5. ['EvenLowerValue1', 34534],
  6. ['EvenLowerValue2', 6343]
  7. ]],
  8. ['LowerValue2', [
  9. ['EvenLowerValue1', [
  10. ['CanEvenGoLower1', 42365]
  11. ]]
  12. ]]
  13. ]],
  14. ['TopValue3', 3463]
  15. ]

我尝试了下面这段代码,但没有取得进展:

  1. 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]]
  2. for index, i in enumerate(parsedData):
  3. if index < len(parsedData)-1:
  4. if i[2] == 0 and index == 0: #case of first item
  5. collapsedList.append(i)
  6. collapsedList[index].pop(2)
  7. elif i[2] == 0 and parsedData[index+1][2] == 0:
  8. collapsedList.append(i)
  9. collapsedList[index].pop(2)
  10. elif i[2] <= parsedData[index+1][2]:
  11. collapsedList.append(i)
  12. print(collapsedList)
  13. if i[2] == parsedData[index+1][2]:
  14. collapsedList[index-1].insert(1,i)
  15. elif i[2] > parsedData[index+1][2]:
  16. collapsedList[index-1].insert(1,i)
  17. else:
  18. 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:

  1. [[&#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:

  1. [
  2. [&#39;TopValue1&#39;, 3453, 0],
  3. [&#39;TopValue2&#39;, &#39;&#39;, 0],
  4. [&#39;LowerValue1&#39;, &#39;&#39;, 1],
  5. [&#39;EvenLowerValue1&#39;, 34534, 2],
  6. [&#39;EvenLowerValue2&#39;, 6343, 2],
  7. [&#39;LowerValue2&#39;, &#39;&#39;, 1],
  8. [&#39;EvenLowerValue1&#39;, &#39;&#39;, 2],
  9. [&#39;CanEvenGoLower1&#39;, 42365, 3],
  10. [&#39;TopValue3&#39;, 3463, 0]
  11. ]

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.

  1. [[&#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:

  1. [
  2. [&#39;TopValue1&#39;, 3453],
  3. [&#39;TopValue2&#39;,
  4. [[&#39;LowerValue1&#39;,
  5. [[&#39;EvenLowerValue1&#39;, 34534],
  6. [&#39;EvenLowerValue2&#39;, 6343]]],
  7. [&#39;LowerValue2&#39;,
  8. [&#39;EvenLowerValue1&#39;,
  9. [&#39;CanEvenGoLower1&#39;, 42365]]]]],
  10. [&#39;TopValue3&#39;, 3463]
  11. ]

I tried this atrocity but it's getting me nowhere

  1. 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]]
  2. for index, i in enumerate(parsedData):
  3. if index &lt; len(parsedData)-1:
  4. if i[2] == 0 and index == 0: #case of first item
  5. collapsedList.append(i)
  6. collapsedList[index].pop(2)
  7. elif i[2] == 0 and parsedData[index+1][2] == 0:
  8. collapsedList.append(i)
  9. collapsedList[index].pop(2)
  10. elif i[2] &lt;= parsedData[index+1][2]:
  11. collapsedList.append(i)
  12. print(collapsedList)
  13. if i[2] == parsedData[index+1][2]:
  14. collapsedList[index-1].insert(1,i)
  15. elif i[2] &gt; parsedData[index+1][2]:
  16. collapsedList[index-1].insert(1,i)
  17. else:
  18. pass

答案1

得分: 1

  1. def make_hierarchy(parsedData):
  2. root = []
  3. stack = [["", root]]
  4. for value1, value2, level in parsedData:
  5. stack[level+1:] = [] # 剪裁堆栈
  6. if type(stack[-1][1]) is not list:
  7. stack[-1][1] = [] # 用新列表替换第二个成员
  8. stack[-1][1].append([value1, value2]) # 插入对
  9. stack.append(stack[-1][1][-1]) # ...同时将此对堆叠
  10. 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:

  1. def make_hierarchy(parsedData):
  2. root = []
  3. stack = [[&quot;&quot;, root]]
  4. for value1, value2, level in parsedData:
  5. stack[level+1:] = [] # clip the stack
  6. if type(stack[-1][1]) is not list:
  7. stack[-1][1] = [] # replace second value with new list
  8. stack[-1][1].append([value1, value2]) # inject pair
  9. stack.append(stack[-1][1][-1]) # ...and also stack this pair
  10. 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:

确定