社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

python列表的dict到dict列表的dict

Boat • 5 年前 • 1531 次点击  

我正试图根据我的需要格式化一个数据结构。我想从一个dict列表创建一个dict列表。 基于这个数据结构:

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

但值得一提的是,它记录了所有的价值。每把钥匙只需要一个发音。很明显我在这里遗漏了一些东西。你能帮我点灯吗?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/43163
 
1531 次点击  
文章 [ 2 ]  |  最新文章 5 年前
Patrick Artner
Reply   •   1 楼
Patrick Artner    6 年前

覆盖数据-需要添加到其中:

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}]}
Stephen Cowley
Reply   •   2 楼
Stephen Cowley    6 年前

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