字典python列表中的组数据

时间:2016-01-08 09:41:26

标签: python dictionary

我有一个像这样的词典列表

data = [
    {"_id": {"cohort_name": "09-01-2010", "segment_name": "LTV90-Prime", "driver_name": "ADB"}, "cohort_data": [
        {"calculated": [],
         "original": [{"1": 225.2699758337715}, {"2": 106.05173118059133}, {"3": 547.2908664469512},
                      {"4": 573.1083659247656}]}]},
    {"_id": {"cohort_name": "11-01-2010", "segment_name": "LTV90-Prime", "driver_name": "Unit Loss Rate"},
     "cohort_data": [{"calculated": [], "original": [{"1": 0.002687180620372531}, {"2": 0.001468127113897437}]}]},
    {"_id": {"cohort_name": "11-01-2010", "segment_name": "LTV90-Prime", "driver_name": "Unit Loss Rate"},
     "cohort_data": [{"calculated": [], "original": [{"10": 0.002687180620372531}, {"1": 0.002687180620372531},
                                                     {"2": 0.001468127113897437}]}]}
]

我正在尝试根据driver_name和segment_name对数据进行分组,并在内部字典中推送所有cohort_name和cohort_data。

预期输出如下

[{'driver_name': 'Unit Loss Rate',
      'segment_name': 'LTV90-Prime',
      'cohort_data': {
          '5-01-2010': [{'1': 0.002687180620372531}, {'2': 0.001468127113897437}, {'10': 0.002687180620372531}],
          '11-01-2010': [{'1': 0.002687180620372531}, {'2': 0.001468127113897437}]
      }},
     {'driver_name': 'ADB',
      'segment_name': 'LTV90-Prime',
      'cohort_data': {
          "09-01-2010": [{'1': 225.2699758337715}, {'2': 106.05173118059133}, {'3': 547.2908664469512},
                         {'4': 573.1083659247656}]
      }}
     ]

这是我到目前为止所做的。我被困在内部字典中推送cohort_name和cohort_data。

def get_data_list(d):
    final_data = None
    for i in d:
        calculated = i['calculated']
        original = i['original']
        if original:
            final_data = original
        elif calculated:
            final_data = calculated

    return final_data


dd = defaultdict(dict)
for i in data:

    df = {}
    id_ = i['_id']
    cohort_name_final, segment_name_final, driver_name_final = id_['cohort_name'], \
                                                               id_['segment_name'], \
                                                               id_['driver_name']

    cohort_data_final = i['cohort_data']

    if segment_name_final not in df and segment_name_final not in df:
        df['segment_name'] = segment_name_final
        df['driver_name'] = driver_name_final
        df['cohort_data'] = get_data_list(cohort_data_final)
    elif segment_name_final in df and segment_name_final in df:
        df['cohort_data'].append(get_data_list(cohort_data_final))
        # df['cohort_data'].append({cohort_name_final: get_data_list(cohort_data_final)})

我正在使用Python 3.4.3。此处显示的数据是从MongoDB数据库查询的原始数据集的子集。

请帮忙。

0 个答案:

没有答案