使用Python创建嵌套的字典或列表,根据提供的非缩进数据。

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

Nested dictionary or list using given unindent data using Python

问题

  1. {
  2. "Current Assets": {
  3. "Cash": {
  4. "Checking": 583961,
  5. "Savings": 224600,
  6. "Petty Cash": 89840,
  7. "Total Cash": 898402
  8. },
  9. "Accounts Receivable": 3593607,
  10. "Work in Process": 589791,
  11. "Other Current Assets": {
  12. "Prepaid Rent": 164593,
  13. "Prepaid Liability Insurance": 109728,
  14. "Total Other Current Assets": 274321
  15. },
  16. "Total Current Assets": 5356121
  17. }
  18. }
英文:

Hi I have given below data but its unindent only using total keyword I can find the right nodes and can build tree structure.
Input:

  1. Current Assets
  2. Cash
  3. Checking 583961
  4. Savings 224600
  5. Petty Cash 89840
  6. Total Cash 898402
  7. Accounts Receivable 3593607
  8. Work in Process 589791
  9. Other Current Assets
  10. Prepaid Rent 164593
  11. Prepaid Liability Insurance 109728
  12. Total Other Current Assets 274321
  13. Total Current Assets 274321

I am looking for below Output:

  1. {
  2. "Current Assets": {
  3. "Cash": {
  4. "Checking": 583961,
  5. "Savings": 224600,
  6. "Petty Cash": 89840,
  7. "Total Cash": 898402
  8. },
  9. "Accounts Receivable": 3593607,
  10. "Work in Process": 589791,
  11. "Other Current Assets": {
  12. "Prepaid Rent": 164593,
  13. "Prepaid Liability Insurance": 109728,
  14. "Total Other Current Assets": 274321
  15. },
  16. "Total Current Assets": 5356121
  17. }
  18. }

I tried recursion and node concept but nothing worked, It will be great if someone can help me on that trying to achieve using Python.

Rules:

As an example :
Actually work in process is not sub item of Account Receivable' Its item of current asset only.
As "work in progress" have digit at its end hence no children of it.

As per input data Cash does not have any numeric value at end hence such entries will have child/children,

cash is ending once having total cash with numeric value.

There will not be any children of work in process or Accounts Receivable as they are ending with Numeric value at end

答案1

得分: 2

可以使用递归函数或者使用一个栈来跟踪嵌套。基本规则是:

  • 没有数字:增加嵌套
  • 以"Total"开头:减少嵌套

使用栈的话,代码可能是这样的:

  1. import re
  2. s = '''Current Assets
  3. Cash
  4. Checking 583961
  5. Savings 224600
  6. Petty Cash 89840
  7. Total Cash 898402
  8. Accounts Receivable 3593607
  9. Work in Process 589791
  10. Other Current Assets
  11. Prepaid Rent 164593
  12. Prepaid Liability Insurance 109728
  13. Total Other Current Assets 274321
  14. Total Current Assets 274321'''
  15. def nest(items):
  16. res = {}
  17. stack = [res]
  18. for item in items:
  19. components = re.findall(r'(^.*?) (\d+)', item)
  20. if not components: # 没有数字
  21. cur = {}
  22. stack[-1][item.strip()] = cur
  23. stack.append(cur)
  24. else:
  25. label, nums = components[0]
  26. stack[-1][label.strip()] = int(nums)
  27. if label.startswith("Total"): # 子字典结束
  28. stack.pop()
  29. return res
  30. nest(s.split('\n'))

这将返回:

  1. {
  2. 'Current Assets': {
  3. 'Cash': {
  4. 'Checking': 583961,
  5. 'Savings': 224600,
  6. 'Petty Cash': 89840,
  7. 'Total Cash': 898402
  8. },
  9. 'Accounts Receivable': 3593607,
  10. 'Work in Process': 589791,
  11. 'Other Current Assets': {
  12. 'Prepaid Rent': 164593,
  13. 'Prepaid Liability Insurance': 109728,
  14. 'Total Other Current Assets': 274321
  15. },
  16. 'Total Current Assets': 274321
  17. }
  18. }
英文:

You can do this with a recursive function or just use a stack to keep track of the nesting. The basic rule is:

  • No number: increase nesting
  • Starts with "Total": decrease nesting.

With a stack, it might look like:

  1. import re
  2. s = '''Current Assets
  3. Cash
  4. Checking 583961
  5. Savings 224600
  6. Petty Cash 89840
  7. Total Cash 898402
  8. Accounts Receivable 3593607
  9. Work in Process 589791
  10. Other Current Assets
  11. Prepaid Rent 164593
  12. Prepaid Liability Insurance 109728
  13. Total Other Current Assets 274321
  14. Total Current Assets 274321'''
  15. def nest(items):
  16. res = {}
  17. stack = [res]
  18. for item in items:
  19. components = re.findall(r'(^.*?) (\d+)', item)
  20. if not components: # no numbers
  21. cur = {}
  22. stack[-1][item.strip()] = cur
  23. stack.append(cur)
  24. else:
  25. label, nums = components[0]
  26. stack[-1][label.strip()] = int(nums)
  27. if label.startswith("Total"): # end of subdict
  28. stack.pop()
  29. return res
  30. nest(s.split('\n'))

This will return:

  1. {
  2. 'Current Assets': {
  3. 'Cash': {
  4. 'Checking': 583961,
  5. 'Savings': 224600,
  6. 'Petty Cash': 89840,
  7. 'Total Cash': 898402
  8. },
  9. 'Accounts Receivable': 3593607,
  10. 'Work in Process': 589791,
  11. 'Other Current Assets': {
  12. 'Prepaid Rent': 164593,
  13. 'Prepaid Liability Insurance': 109728,
  14. 'Total Other Current Assets': 274321
  15. },
  16. 'Total Current Assets': 274321
  17. }
  18. }

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

发表评论

匿名网友

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

确定