代码之家  ›  专栏  ›  技术社区  ›  prashant thakre

使用Python使用给定的非凹痕数据的嵌套字典或列表

  •  -1
  • prashant thakre  · 技术社区  · 3 年前

    嗨,我已经给出了以下数据,但它没有影响,只使用total关键字我可以找到正确的节点,并可以构建树结构。 输入:

    Current Assets  
    Cash  
    Checking 583961  
    Savings 224600  
    Petty Cash 89840  
    Total Cash 898402  
    Accounts Receivable 3593607  
    Work in Process 589791  
    Other Current Assets  
    Prepaid Rent 164593  
    Prepaid Liability Insurance 109728  
    Total Other Current Assets 274321  
    Total Current Assets 274321  
    

    我正在寻找以下输出:

    {
        "Current Assets": {
            "Cash": {
                "Checking": 583961,
                "Savings": 224600,
                "Petty Cash": 89840,
                "Total Cash": 898402
            },
            "Accounts Receivable": 3593607,
            "Work in Process": 589791,
            "Other Current Assets": {
                "Prepaid Rent": 164593,
                "Prepaid Liability Insurance": 109728,
                "Total Other Current Assets": 274321
            },
            "Total Current Assets": 5356121
        }
    }
    

    我尝试了递归和节点概念,但没有成功,如果有人能帮助我使用Python实现这一点,那将是一件很棒的事情。

    规则:

    例如: 事实上 work in process 不是的子项 Account Receivable' Its item of 仅限流动资产。 由于“正在进行的工作”的末尾有数字,因此没有它的子项。

    根据输入数据,Cash在末尾不具有任何数值,

    现金一旦有了带有数字值的现金总额就结束了。

    不会有任何在制品或应收账款的子项,因为它们以数字值结尾

    1 回复  |  直到 3 年前
        1
  •  2
  •   Mark    3 年前

    您可以使用递归函数来完成此操作,也可以只使用堆栈来跟踪嵌套。基本规则是:

    • 无编号:增加嵌套
    • 以“总计”开头:减少嵌套。

    对于堆栈,它可能看起来像:

    import re
    
    s = '''Current Assets  
    Cash  
    Checking 583961  
    Savings 224600  
    Petty Cash 89840  
    Total Cash 898402  
    Accounts Receivable 3593607  
    Work in Process 589791  
    Other Current Assets  
    Prepaid Rent 164593  
    Prepaid Liability Insurance 109728  
    Total Other Current Assets 274321  
    Total Current Assets 274321'''
    
    
    def nest(items):
        res = {}
        stack = [res]
        for item in items:
            components = re.findall(r'(^.*?) (\d+)', item)
            if not components: # no numbers
                cur = {}
                stack[-1][item.strip()] = cur
                stack.append(cur)
            else:
                label, nums = components[0]
                stack[-1][label.strip()] = int(nums)
                if label.startswith("Total"): # end of subdict
                    stack.pop()
        return res
            
    nest(s.split('\n'))
    

    这将返回:

    {
      'Current Assets': {
         'Cash': {
               'Checking': 583961,
               'Savings': 224600,
               'Petty Cash': 89840,
               'Total Cash': 898402
          },
          'Accounts Receivable': 3593607,
          'Work in Process': 589791,
          'Other Current Assets': { 
               'Prepaid Rent': 164593,
               'Prepaid Liability Insurance': 109728,
               'Total Other Current Assets': 274321
          },
          'Total Current Assets': 274321
      }
    }