fork download
  1. def calculate_ltv (query_results, aggregation_method, profiles):
  2. responses_by_id = {}
  3.  
  4. for i in profiles:
  5. if aggregation_method == "count" or aggregation_method == "sum":
  6. # default all entries for counted or summed aggregations to 0 to start... will be replaced later if there is a response
  7. responses_by_id[i["id"]] = [0]
  8.  
  9. for row in query_results:
  10. responses_by_id[row["id"]] = row["data"]
  11.  
  12. return responses_by_id
  13.  
  14.  
  15. mock_query_results = [{"id":1, "data":[112.50]}, {"id":2, "data":[16.48]}, {"id":3, "data":[42.97]}, {"id":4, "data":[96.41]}]
  16. mock_aggregation_method = "sum"
  17. mock_profiles = [{"id": 1}, {"id": 2}, {"id": 3}, {"id": 4}, {"id": 5}]
  18.  
  19.  
  20. print(calculate_ltv(mock_query_results, mock_aggregation_method, mock_profiles))
  21.  
Success #stdin #stdout 0.02s 9300KB
stdin
Standard input is empty
stdout
{1: [112.5], 2: [16.48], 3: [42.97], 4: [96.41], 5: [0]}