繁体   English   中英

python字典列表到列表的字典

[英]python List of dict to dict of dict of list

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

[{'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

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

我建议使用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}]})

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

 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)

(或使用collections中defaultdict )比使用dict.setdefault(...)更为有效-但对于您的datasample来说,两者都应该有效。


输出:

{'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}]}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM