将逗号分隔的数值拆分为子节点。

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

Break down comma separated values into children nodes

问题

  1. 我有一个列表格式如下

[[2020,
'September',
9,
'12:00',
'1',
28,
'Breaking',
7.0,
'Request Body Type Changed,API Path Removed Without Deprecation,Request Property Removed,Response Property Became Optional,Response Success Status Removed']]

  1. 这是我用列表创建的层次结构:
  2. ```python
  3. {
  4. 'name': '12:00',
  5. 'children': [
  6. {
  7. 'name': '1',
  8. 'value': 28,
  9. 'children': [
  10. {
  11. 'name': 'Breaking',
  12. 'value': 7.0,
  13. 'children': [],
  14. 'hidden_children': [
  15. {
  16. 'name': 'Request Body Type Changed,API Path Removed Without Deprecation,Request Property Removed,Response Property Became Optional,Response Success Status Removed',
  17. 'value': 7.0,
  18. 'itemStyle': {'color': '#ad213e'},
  19. 'label': {'rotate': 0}
  20. }
  21. ],
  22. 'itemStyle': {'color': '#ad213e'}
  23. }
  24. ]
  25. }
  26. ]
  27. }

但正如你所看到的,我把所有的子节点放在了逗号分隔的格式中。我想把它们作为单独的子节点放在层次结构中。这意味着对于Breaking父节点,不是一个hidden_children节点,而是4个hidden_children节点。

期望的层次结构是:

  1. {
  2. 'name': '12:00',
  3. 'children': [
  4. {
  5. 'name': '1',
  6. 'value': 28,
  7. 'children': [
  8. {
  9. 'name': 'Breaking',
  10. 'value': 7.0,
  11. 'children': [],
  12. 'hidden_children': [
  13. {
  14. 'name': 'Request Body Type Changed',
  15. 'value': 7.0,
  16. 'itemStyle': {'color': '#ad213e'},
  17. 'label': {'rotate': 0}
  18. },
  19. {
  20. 'name': 'API Path Removed Without Deprecation',
  21. 'value': 7.0,
  22. 'itemStyle': {'color': '#ad213e'},
  23. 'label': {'rotate': 0}
  24. },
  25. {
  26. 'name': 'Request Property Removed',
  27. 'value': 7.0,
  28. 'itemStyle': {'color': '#ad213e'},
  29. 'label': {'rotate': 0}
  30. },
  31. {
  32. 'name': 'Response Property Became Optional',
  33. 'value': 7.0,
  34. 'itemStyle': {'color': '#ad213e'},
  35. 'label': {'rotate': 0}
  36. },
  37. {
  38. 'name': 'Response Success Status Removed',
  39. 'value': 7.0,
  40. 'itemStyle': {'color': '#ad213e'},
  41. 'label': {'rotate': 0}
  42. }
  43. ],
  44. 'itemStyle': {'color': '#ad213e'}
  45. }
  46. ]
  47. }
  48. ]
  49. }

我不确定如何将字符串拆分成不同的子节点来绘制它们。

这是我创建这个层次结构的代码:

  1. def create_hierarchy(api1):
  2. # ...
  3. # (代码未变)
  4. # ...
  5. if content:
  6. if "," in content:
  7. # ...
  8. # (代码未变)
  9. # ...
  10. else:
  11. content_node = {'name': content, 'value': count}
  12. if type1 == 'Breaking':
  13. content_node['itemStyle'] = {'color': '#ad213e'}
  14. content_node['label'] = {'rotate': 0}
  15. type_node['hidden_children'].append(content_node)
  16. else:
  17. type_node['children'].append(content_node)
  18. type_node['itemStyle'] = {'color': '#ad213e'}
  19. return root['children']
  20. # (代码未变)
  21. # ...
  1. hierarchy1 = create_hierarchy(api1)

这里的api1是我的列表的名称。

是否有人有建议或者想法,我该如何做到这一点?

  1. <details>
  2. <summary>英文:</summary>
  3. I have a list which is of this format:

[[2020,
'September',
9,
'12:00',
'1',
28,
'Breaking',
7.0,
'Request Body Type Changed,API Path Removed Without Deprecation,Request Property Removed,Response Property Became Optional,Response Success Status Removed']]

  1. This is the hierarchy I have created with the list:
  1. {
  2. &#39;name&#39;: &#39;12:00&#39;,
  3. &#39;children&#39;: [
  4. {
  5. &#39;name&#39;: &#39;1&#39;,
  6. &#39;value&#39;: 28,
  7. &#39;children&#39;: [
  8. {
  9. &#39;name&#39;: &#39;Breaking&#39;,
  10. &#39;value&#39;: 7.0,
  11. &#39;children&#39;: [
  12. ],
  13. &#39;hidden_children&#39;: [
  14. {
  15. &#39;name&#39;: &#39;Request Body Type Changed,API Path Removed Without Deprecation,Request Property Removed,Response Property Became Optional,Response Success Status Removed&#39;,
  16. &#39;value&#39;: 7.0,
  17. &#39;itemStyle&#39;: {
  18. &#39;color&#39;: &#39;#ad213e&#39;
  19. },
  20. &#39;label&#39;: {
  21. &#39;rotate&#39;: 0
  22. }
  23. }
  24. ],
  25. &#39;itemStyle&#39;: {
  26. &#39;color&#39;: &#39;#ad213e&#39;
  27. }
  28. }
  29. ]
  30. }
  31. ]
  32. }
  1. However as you can see I have all the children in a comma separated format. What I want to do this put them as separate children in the hierarchy. Which means instead of one `hidden_children` node for `Breaking` parent node, there will be `4 hidden_children`.
  2. The expected hierarchy is:
  1. {
  2. &#39;name&#39;: &#39;12:00&#39;,
  3. &#39;children&#39;: [
  4. {
  5. &#39;name&#39;: &#39;1&#39;,
  6. &#39;value&#39;: 28,
  7. &#39;children&#39;: [
  8. {
  9. &#39;name&#39;: &#39;Breaking&#39;,
  10. &#39;value&#39;: 7.0,
  11. &#39;children&#39;: [],
  12. &#39;hidden_children&#39;: [
  13. {
  14. &#39;name&#39;: &#39;Request Body Type Changed&#39;,
  15. &#39;value&#39;: 7.0,
  16. &#39;itemStyle&#39;: {&#39;color&#39;: &#39;#ad213e&#39;},
  17. &#39;label&#39;: {&#39;rotate&#39;: 0}
  18. },
  19. {
  20. &#39;name&#39;: &#39;API Path Removed Without Deprecation&#39;,
  21. &#39;value&#39;: 7.0,
  22. &#39;itemStyle&#39;: {&#39;color&#39;: &#39;#ad213e&#39;},
  23. &#39;label&#39;: {&#39;rotate&#39;: 0}
  24. },
  25. {
  26. &#39;name&#39;: &#39;Request Property Removed&#39;,
  27. &#39;value&#39;: 7.0,
  28. &#39;itemStyle&#39;: {&#39;color&#39;: &#39;#ad213e&#39;},
  29. &#39;label&#39;: {&#39;rotate&#39;: 0}
  30. },
  31. {
  32. &#39;name&#39;: &#39;Response Property Became Optional&#39;,
  33. &#39;value&#39;: 7.0,
  34. &#39;itemStyle&#39;: {&#39;color&#39;: &#39;#ad213e&#39;},
  35. &#39;label&#39;: {&#39;rotate&#39;: 0}
  36. },
  37. {
  38. &#39;name&#39;: &#39;Response Success Status Removed&#39;,
  39. &#39;value&#39;: 7.0,
  40. &#39;itemStyle&#39;: {&#39;color&#39;: &#39;#ad213e&#39;},
  41. &#39;label&#39;: {&#39;rotate&#39;: 0}
  42. }
  43. ],
  44. &#39;itemStyle&#39;: {&#39;color&#39;: &#39;#ad213e&#39;}
  45. }
  46. ]
  47. }
  48. ]
  49. }
  1. I am not sure how can I split the string to graph them out as different children nodes.
  2. This is my code for creating this hierarchy:

def create_hierarchy(api1):
root = {"name": "root", "children": []}
for row in api1:
year, month, day, time, info_version, api_changes, type1, count, content = row + [None] * (9 - len(row))
year_node = None
for node in root["children"]:
if node["name"] == year:
year_node = node
break
if not year_node:
year_node = {"name": year, "children": []}
root["children"].append(year_node)
month_node = None
for node in year_node["children"]:
if node["name"] == month:
month_node = node
break
if not month_node:
month_node = {"name": month, "children": []}
year_node["children"].append(month_node)
day_node = None
for node in month_node["children"]:
if node["name"] == str(day):
day_node = node
break
if not day_node:
day_node = {"name": str(day), "children": []}
month_node["children"].append(day_node)
hour_node = {"name": str(time), "children": []}
day_node["children"].append(hour_node)
info_node = {"name": info_version, "value": api_changes, "children": []}
hour_node["children"].append(info_node)
if type1:
type_node = None
for node in info_node["children"]:
if node["name"] == type1:
type_node = node
break
if not type_node:
type_node = {"name": type1, "value": count, "children": []}
info_node["children"].append(type_node)
if type1 == "Breaking":
type_node["hidden_children"] = []
type_node["itemStyle"] = {"color": "#ad213e"}
if content:
if "," in content:
content_list = content.split(", ")
for content in content_list:
content_node = {"name": content, "value": count}
if type1 == "Breaking":
content_node["itemStyle"] = {"color": "#ad213e"}
content_node["label"] = {"rotate": 0}
type_node["hidden_children"].append(content_node)
else:
type_node["children"].append(content_node)
type_node["itemStyle"] = {"color": "#ad213e"}
else:
content_node = {"name": content, "value": count}
if type1 == "Breaking":
content_node["itemStyle"] = {"color": "#ad213e"}
content_node["label"] = {"rotate": 0}
type_node["hidden_children"].append(content_node)
else:
type_node["children"].append(content_node)
type_node["itemStyle"] = {"color": "#ad213e"}
return root["children"]

hierarchy1 = create_hierarchy(api1)

  1. `api1` here is the name of my list.
  2. Does anyone have suggestions or ideas how I could do this?
  3. </details>
  4. # 答案1
  5. **得分**: 1
  6. 使用列表推导来创建包含逗号分隔字符串中每个元素的字典列表。
  7. 通过使用一个变量来保存键(要么是`children`要么是`hidden_children`)来避免所有重复的`if`语句中的重复代码。
  8. ```python
  9. if content:
  10. children_key = "hidden_children" if type1 == "Breaking" else "children"
  11. content_list = content.split(",")
  12. type_node[children_key] = [
  13. {"name": c, "value": count, "itemStyle": {"color": "#ad213e"}, "label": {"rotate": 0}}
  14. for c in content_list]

无需测试content中是否存在逗号,因为split()会返回一个包含一个字符串的列表,如果逗号不存在,遍历它将返回一个包含一个子项的列表。

英文:

Use a list comprehension to create the list of dictionaries for each element of the comma-separated string.

Avoid all the duplicate code in if statements by using a variable to hold the key, either children or hidden_children.

  1. if content:
  2. children_key = &quot;hidden_children&quot; if type1 == &quot;Breaking&quot; else &quot;children&quot;
  3. content_list = content.split(&quot;,&quot;)
  4. type_node[children_key] = [
  5. {&quot;name&quot;: c, &quot;value&quot;: count, &quot;itemStyle&quot;: {&quot;color&quot;: &quot;#ad213e&quot;}, &quot;label&quot;: {&quot;rotate&quot;: 0}}
  6. for c in content_list]

There's no need for testing whether comma is in content, because split() will return a list of one string if it doesn't exist, and looping over that will return a list of one child.

huangapple
  • 本文由 发表于 2023年6月2日 07:05:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76386224.html
匿名

发表评论

匿名网友

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

确定