最佳实践是更新包含列表和字典的字典列表中的字段。

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

best practice to update a field in list of dictionary of list and dictionary

问题

你想要更新这个部分,将"aaaa"和"bbb"添加进去:

  1. "instance[]": [
  2. {
  3. "key": "requires",
  4. "value": {
  5. "string[]": ["aaaa", "bbb"]
  6. }

你可以使用以下方法来更新它:

  1. # 找到包含"key"为"order"的字典
  2. newl = [d for d in test if d["key"] == "order"]
  3. # 检查是否找到了匹配的字典
  4. if newl:
  5. # 在匹配的字典中找到"instance[]"部分
  6. instance_list = newl[0]["value"]["instance[]"]
  7. # 在"requires"字典中添加"aaaa"和"bbb"
  8. for instance in instance_list:
  9. if instance["key"] == "order_det":
  10. instance["value"]["instance[]"][0]["value"]["string[]"].extend(["aaaa", "bbb"])
  11. # 打印更新后的test列表
  12. print(test)

这将会更新你的test列表,添加"aaaa"和"bbb"到指定的位置。

英文:

Assuming the input is a list of dictionary.
I need to update a specific field with new values.
There are repeated keys, values in the input so cannot distinguish the field need to be picked by its name.
Here is an example:

  1. test = [
  2. {
  3. "key": "prog",
  4. "value": {
  5. "instance[]": [
  6. {
  7. "key": "name",
  8. "value": {
  9. "string": "NAME"
  10. },
  11. "verif": 22222222
  12. },
  13. {
  14. "key": "user",
  15. "value": {
  16. "string": "AAAAA"
  17. },
  18. "verif": 22222222
  19. }
  20. ]
  21. },
  22. "verif": 22222222
  23. },
  24. {
  25. "key": "sytem_platform",
  26. "value": {
  27. "bool": "false"
  28. },
  29. "verif": 22222222
  30. },
  31. {
  32. "key": "system_beh",
  33. "value": {
  34. "bool": "true"
  35. },
  36. "verif": 22222222
  37. },
  38. {
  39. "key": "check_beh",
  40. "value": {
  41. "bool": "true"
  42. },
  43. "verif": 22222222
  44. },
  45. {
  46. "key": "order",
  47. "value": {
  48. "instance[]": [
  49. {
  50. "key": "order_det",
  51. "value": {
  52. "instance[]": [
  53. {
  54. "key": "requires",
  55. "value": {
  56. "string[]": []
  57. },
  58. "verif": 22222222
  59. },
  60. {
  61. "key": "status",
  62. "value": {
  63. "string[]": []
  64. },
  65. "verif": 22222222
  66. },
  67. {
  68. "key": "system_status",
  69. "value": {
  70. "string[]": [
  71. "sys1.out",
  72. "sys1.checking"
  73. ]
  74. },
  75. "verif": 22222222
  76. },
  77. {
  78. "key": "weather_status",
  79. "value": {
  80. "instance[]": [
  81. {
  82. "key": "humdiity",
  83. "value": {
  84. "double": 5.0
  85. },
  86. "verif": 22222222
  87. },
  88. {
  89. "key": "temp",
  90. "value": {
  91. "double": 70.0
  92. },
  93. "verif": 22222222
  94. }
  95. ]
  96. },
  97. "verif": 22222222
  98. },
  99. {
  100. "key": "environment",
  101. "value": {
  102. "string[]": []
  103. },
  104. "verif": 22222222
  105. }
  106. ]
  107. },
  108. "verif": 22222222
  109. }
  110. ]
  111. },
  112. "verif": 22222222
  113. }

]

I am trying to update the following section of it like adding "aaaa" and "bbb"

  1. "instance[]": [
  2. {
  3. "key": "requires",
  4. "value": {
  5. "string[]": ["aaaa", "bbb"]
  6. },

I was wondering if there is any built-in library in python that i can use for this goal?

i can filter it out and store it in a list like below, but i am not sure how i can update it.

  1. newl = [d for d in test if d["key"] == "order" ]
  2. print(newl)
  3. #which prints
  4. [{'key': 'order', 'value': {'instance[]': [{'key': 'order_det', 'value': {'instance[]': [{'key': 'requires', 'value': {'string[]': []}, 'verif': 22222222}, {'key': 'status', 'value': {'string[]': []}, 'verif': 22222222}, {'key': 'system_status', 'value': {'string[]': ['sys1.out', 'sys1.checking']}, 'verif': 22222222}, {'key': 'weather_status', 'value': {'instance[]': [{'key': 'humdiity', 'value': {'double': 5.0}, 'verif': 22222222}, {'key': 'temp', 'value': {'double': 70.0}, 'verif': 22222222}]}, 'verif': 22222222}, {'key': 'environment', 'value': {'string[]': []}, 'verif': 22222222}]}, 'verif': 22222222}]}, 'verif': 22222222}]

答案1

得分: 1

你可以使用递归来查找具有指定键的字典。

  1. def get(o, k):
  2. if isinstance(o, list):
  3. for x in o:
  4. if res := get(x, k):
  5. return res
  6. elif o['key'] == k:
  7. return o
  8. elif nxt := o.get('value', {}).get('instance[]'):
  9. return get(nxt, k)
  10. o = get(test, 'requires')
  11. if o:
  12. o['value']['string[]'] += "aaaa", "bbb"
  13. print(test) # test now changed

(Note: I've provided the translated code as requested.)

英文:

You could use recursion to find the dict with the specified key.

  1. def get(o, k):
  2. if isinstance(o, list):
  3. for x in o:
  4. if res := get(x, k):
  5. return res
  6. elif o['key'] == k:
  7. return o
  8. elif nxt := o.get('value', {}).get('instance[]'):
  9. return get(nxt, k)
  10. o = get(test, 'requires')
  11. if o:
  12. o['value']['string[]'] += "aaaa", "bbb"
  13. print(test) # test now changed

huangapple
  • 本文由 发表于 2023年6月5日 04:34:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402313.html
匿名

发表评论

匿名网友

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

确定