Merge list of dict and add new values

  dictionary, json, list, merge, python

I need merge 2 list of dicts by conditions without using pandas

x1 – fact values

x1 = [{'id': '94ffe1d6-0afa-11ec-b139-4cd98f4d62e3',
  'fact_group': '0.05',
  'probability': 0.2},
 {'id': '86ae0229-0af8-11ec-be8c-4cd98f847094',
  'fact_group': '0.05',
  'probability': 0.56},
 {'id': '867ef7ac-0af8-11ec-be8c-4cd98f847094',
  'fact_group': '0.2',
  'probability': 0.31},
 {'id': '211bc00c-0af6-11ec-b139-4cd98f4d62e3',
  'fact_group': '0.2',
  'probability': 0.96}]

x2 – list of dict with intervals probability to labels point

x2 = [{'group': 0.05,
  'predict_labels': 0,
  'predict_intervals_min': 0.00,
  'predict_intervals_max': 0.6},
 {'group': 0.05,
  'predict_labels': 1,
  'predict_intervals_min': 0.6,
  'predict_intervals_max': 1.0},
 {'group': 0.2,
  'predict_labels': 2,
  'predict_intervals_min': 0.0,
  'predict_intervals_max': 0.45},
 {'group': 0.2,
  'predict_labels': 3,
  'predict_intervals_min': 0.45,
  'predict_intervals_max': 1.0}]

I need to merge them by x1['fact_group'] & x2['group']
and x1['probability']>=x2['predict_intervals_min']
and x1['probability']<x2['predict_intervals_max']

Expected: update x1 by x2[‘predict_labels’] by thresholds and group

x3 = [{'id': '94ffe1d6-0afa-11ec-b139-4cd98f4d62e3',
  'predict_labels': 1,
  'fact_group': '0.05',
 'predict_labels': 0,
  'probability': 0.2},
 {'id': '86ae0229-0af8-11ec-be8c-4cd98f847094',
  'predict_labels': 1,
  'fact_group': '0.05',
  'predict_labels': 0,
  'probability': 0.56},
 {'id': '867ef7ac-0af8-11ec-be8c-4cd98f847094',
  'fact_group': '0.2',
  'predict_labels': 2,
  'probability': 0.31},
 {'id': '211bc00c-0af6-11ec-b139-4cd98f4d62e3',
  'fact_group': '0.2',
  'predict_labels': 3,
  'probability': 0.96}]

Source: Python Questions

LEAVE A COMMENT