2016-04-27 34 views
2

我想連接多個字典來返回我的函數。python聯盟字典

def get_params(request, active_id): 

    tab_parameters = [ 
     shadow(active_id), 
     light_parameter(active_id) 
    ] 

    parameters = { 
     'id': active_id, 
     'parameters': tab_parameters 
    } 

    return response.HttpResponse(json.dumps(parameters), 'application/json') 


def light_parameter(active_id): 
    active_lights = LightParameter.objects.filter(id=active_id).all() 

    list_parameters = [] 
    for active_light in active_lights: 
     list_parameters.append({ 
       'name': 'light', 
       'color': active_light.color, 
     }) 

    return list_parameters 

預期的結果是:

{ 
    "id": 1, 
    "parameters": [ 
     { 
      "name": "shadow" 
     }, 
     { 
      "color": 1, 
      "name": "light" 
     }, 
     { 
      "color": 2, 
      "name": "light" 
     } 
} 

我的實際效果(用[]):

{ 
    "id": 1, 
    "parameters": [ 
     { 
      "name": "shadow" 
     }, 
     [ 
      { 
       "color": 1, 
       "name": "light" 
      }, 
      { 
       "color": 2, 
       "name": "light" 
      } 
     ] 
} 

任何想法?我已經嘗試過使用union,update,concatenation但沒有成功。

謝謝!

回答

1

這並不涉及dict s。您只需要正確合併組成tab_parameters的列表即可。

tab_parameters = [shadow(active_id)] + light_parameter(active_id) 
+0

太棒了!非常感謝 :) – Chaoxys