2016-10-04 98 views
0

我創建了一個應該將嵌套列表轉換爲字典的類。以下是我輸入:遞歸修改字典

['function:and', 
    ['variable:X', 'function:>=', 'value:13'], 
    ['variable:Y', 'function:==', 'variable:W']] 

和輸出應該是以下形式的字典:

{ 
    "function": "and", 
    "args": [ 
    { 
     "function": ">=", 
     "args": [ 
     { 
      "variable": "X" 
     }, 
     { 
      "value": 13 
     } 
     ] 
    }, 
    { 
     "function": "==", 
     "args": [ 
     { 
      "variable": "Y" 
     }, 
     { 
      "variable": "W" 
     } 
     ] 
    } 
    ] 
} 

這是接收輸入列表,並應返回所需的字典類。

class Tokenizer(object): 
    def __init__(self, tree): 
     self.tree = tree 
     self.filter = {} 

    def to_dict(self, triple): 
     my_dict = {} 
     try: 
      first = triple[0] 
      second = triple[1] 
      third = triple[2] 
     except KeyError: 
      return 
     if type(second) == str and type(third) == str: 
      my_dict['function'] = second.split(':')[-1] 
      my_dict['args'] = [ 
       {first.split(':')[0]: first.split(':')[1]}, 
       {third.split(':')[0]: third.split(':')[1]}] 
     # case recursive 
     if type(second) == list: 
      my_dict['function'] = first.split(':')[-1] 
      my_dict['args'] = [second, third] 
     return my_dict 

    def walk(self, args): 
     left = self.to_dict(args[0]) 
     right = self.to_dict(args[1]) 
     if isinstance(left, dict): 
      if 'args' in left.keys(): 
       left = self.walk(left['args']) 
     if isinstance(right, dict): 
      if 'args' in right.keys(): 
       right = self.walk(right['args']) 
     args = [left, right] 
     return args 

    def run(self): 
     self.filter.update(self.to_dict(self.tree)) 
     if 'args' in self.filter.keys(): 
      self.filter['args'] = self.walk(self.filter['args']) 


tree = [ 
    'function:and', 
     ['variable:X', 'function:>=', 'value:13'], 
     ['variable:Y', 'function:==', 'variable:W'] 
    ] 

import pprint 
pp = pprint.PrettyPrinter(indent=4) 
t = Tokenizer(tree) 
t.run() 
pp.pprint(t.filter) 

我的遞歸方法walk沒有做它應該是什麼,我在遞歸共吸盤,所以我不能圖什麼我做錯了。

我得到的輸出是:

{ 'args': [[None, None], [None, None]], 'function': 'and'} 
+0

_我的方法沒有做它應該做的事那麼它在做什麼呢? –

+0

@JohnGordon:編輯添加輸出,謝謝! – PepperoniPizza

+0

可能是https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ –

回答

1

對於不需要進入遞歸在所有特定的測試用例。您可以發表您的電話評論:

def walk(self, args): 
    left = self.to_dict(args[0]) 
    right = self.to_dict(args[1]) 
    #if isinstance(left, dict): 
    # if 'args' in left.keys(): 
    #  left = self.walk(left['args']) 
    #if isinstance(right, dict): 
    # if 'args' in right.keys(): 
    #  right = self.walk(right['args']) 
    args = [left, right] 
    return args 

並獲得所需的輸出。 你只需要進入遞歸,如果你允許你輸入嵌套函數:

['function:and', 
     ['variable:X', 'function:>=', 'value:13'], 
     ['function:==', 
      ['variable:R', 'function:>=', 'value:1'], 
      ['variable:Z', 'function:==', 'variable:K'] 
     ] 
] 

,那麼你必須檢查基本情況,讓你進入遞歸只有當你的args鍵的值包含未處理值:

def walk(self, args): 
    left = self.to_dict(args[0]) 
    right = self.to_dict(args[1]) 
    if isinstance(left, dict): 
     if 'args' in left.keys() and isinstance(left['args'][0], list): 
      left = self.walk(left['args']) 
    if isinstance(right, dict): 
     if 'args' in right.keys() and isinstance(right['args'][0], list): 
      right = self.walk(right['args']) 
    args = [left, right] 
    return args 

,然後你會得到這樣的:

{ 'args': [ { 'args': [{ 'variable': 'X'}, { 'value': '13'}], 
       'function': '>='}, 
      { 'args': [ { 'args': [ { 'variable': 'R'}, 
              { 'value': '1'}], 
           'function': '>='}, 
          { 'args': [ { 'variable': 'Z'}, 
              { 'variable': 'K'}], 
           'function': '=='}], 
       'function': '=='}], 
'function': 'and'} 

而且它會更容易,如果你的輸入列表是一個普通結構在函數名字段後面始終有參數字段。然後,您可以大大簡化您的to_dict方法。