python字典列表到列表的字典

时间:2018-11-27 17:44:51

标签: python list dictionary

我正在尝试根据需要格式化数据结构。我想从字典列表中创建列表字典的字典。 基于该数据结构:

[{'accuracy': 0.04584040881114014,
  'epoch': 0,
  'loss': 3.908684137519228,
  'name': 'test14',
  'val_accuracy': 0.1878172606229782,
  'val_loss': 3.8432216644287114},
 {'accuracy': 0.1612903245539738,
  'epoch': 1,
  'loss': 3.8308442072066873,
  'name': 'test14',
  'val_accuracy': 0.1878172606229782,
  'val_loss': 3.4720854759216313},
 {'accuracy': 0.056027164736506485,
  'epoch': 0,
  'loss': 3.9064058800099866,
  'name': 'test15',
  'val_accuracy': 0.1878172606229782,
  'val_loss': 3.8064255714416495},
 {'accuracy': 0.16129032566713356,
  'epoch': 1,
  'loss': 3.7856348285448367,
  'name': 'test15',
  'val_accuracy': 0.1878172606229782,
  'val_loss': 3.5590925216674805}]

我想实现这样的目标:

    {'test14': [{'accuracy': 0.04584040881114014,
       'epoch': 0,
       'loss': 3.908684137519228,
       'val_accuracy': 0.1878172606229782,
       'val_loss': 3.8432216644287114},
      {'accuracy': 0.1612903245539738,
       'epoch': 1,
       'loss': 3.8308442072066873,
       'val_accuracy': 0.1878172606229782,
       'val_loss': 3.4720854759216313}],
     'test15': [{'accuracy': 0.056027164736506485,
       'epoch': 0,
       'loss': 3.9064058800099866,
       'name': 'test15',
       'val_accuracy': 0.1878172606229782,
       'val_loss': 3.8064255714416495},
      {'accuracy': 0.16129032566713356,
       'epoch': 1,
       'loss': 3.7856348285448367,
       'name': 'test15',
       'val_accuracy': 0.1878172606229782,
       'val_loss': 3.5590925216674805}]}

So far I succeed to have something like that :

{'test14': {'accuracy': 0.1612903245539738,
  'epoch': 1,
  'loss': 3.8308442072066873,
  'val_accuracy': 0.1878172606229782,
  'val_loss': 3.4720854759216313},
 'test15': {'accuracy': 0.16129032566713356,
  'epoch': 1,
  'loss': 3.7856348285448367,
  'val_accuracy': 0.1878172606229782,
  'val_loss': 3.5590925216674805}}

使用这段代码:

for entry in data:
         name = entry.pop('name')
         n_dict[name] = entry

但请注意,要充分利用所有价值。每个键只需要一本字典。我显然在这里错过了一些东西。你能帮我照一下吗?

2 个答案:

答案 0 :(得分:2)

我建议使用defaultdict

from collections import defaultdict

new_dict = defaultdict(list) # New entries will automatically be empty lists
for data_dict in old_list: # Cycle through your old data structure
    new_dict[data_dict['name']].append(data_dict) # Append to the list in the defaultdict with the key testname

这将产生:

defaultdict(list,
        {'test14': [{'accuracy': 0.04584040881114014,
           'epoch': 0,
           'loss': 3.908684137519228,
           'name': 'test14',
           'val_accuracy': 0.1878172606229782,
           'val_loss': 3.8432216644287114},
          {'accuracy': 0.1612903245539738,
           'epoch': 1,
           'loss': 3.8308442072066873,
           'name': 'test14',
           'val_accuracy': 0.1878172606229782,
           'val_loss': 3.4720854759216313}],
         'test15': [{'accuracy': 0.056027164736506485,
           'epoch': 0,
           'loss': 3.9064058800099866,
           'name': 'test15',
           'val_accuracy': 0.1878172606229782,
           'val_loss': 3.8064255714416495},
          {'accuracy': 0.16129032566713356,
           'epoch': 1,
           'loss': 3.7856348285448367,
           'name': 'test15',
           'val_accuracy': 0.1878172606229782,
           'val_loss': 3.5590925216674805}]})

答案 1 :(得分:2)

您覆盖了数据-您需要添加数据:

for entry in data:
     name = entry.pop('name')
     n_dict[name] = entry  # this overwrites the value of name all the time

修复:

n_dict = {}
for entry in data:
     name = entry.pop('name')
     n_dict.setdefault(name,[])
     n_dict[name].append(entry)

(或使用defaultdict from collections)比使用dict.setdefault(...)更有效-但对于您的数据样本,两者均应有效。


输出:

{'test15': [{'loss': 3.9064058800099866, 'val_accuracy': 0.1878172606229782,
             'epoch': 0, 'val_loss': 3.8064255714416495, 
             'accuracy': 0.056027164736506485}, 
            {'loss': 3.7856348285448367, 'val_accuracy': 0.1878172606229782,
             'epoch': 1, 'val_loss': 3.5590925216674805, 
             'accuracy': 0.16129032566713356}], 
 'test14': [{'loss': 3.908684137519228, 'val_accuracy': 0.1878172606229782, 
             'epoch': 0, 'val_loss': 3.8432216644287114, 
             'accuracy': 0.04584040881114014}, 
            {'loss': 3.8308442072066873, 'val_accuracy': 0.1878172606229782, 
             'epoch': 1, 'val_loss': 3.4720854759216313, 
             'accuracy': 0.1612903245539738}]}