Python-将dict转换为嵌套dict

Python-将dict转换为嵌套dict,python,dictionary,Python,Dictionary,我有一句格言: 或 我想把这个dict转换成嵌套的dict,比如 {'Logistic Regression': {'APAR Information':'0.74','1.00','0.85','844'}, {'Affected Products and Versions':'0.00','0.00','0.00','18'} . . .} 如何做到这一点?可以通过dict内置函数来完成吗?这是一种方法 演示: d = {'Logistic Regression': u'

我有一句格言:

我想把这个dict转换成嵌套的dict,比如

{'Logistic Regression':
{'APAR Information':'0.74','1.00','0.85','844'},
{'Affected Products and Versions':'0.00','0.00','0.00','18'}
.
.
.}
如何做到这一点?可以通过dict内置函数来完成吗?

这是一种方法

演示:

d = {'Logistic Regression': u'                                precision    recall  f1-score   support\n\n              APAR Information       0.74      1.00      0.85       844\nAffected Products and Versions       0.00      0.00      0.00        18\n                        Answer       0.00      0.00      0.00        30\n   Applicable component levels       0.96      0.85      0.90       241\n             Error description       0.48      0.56      0.52       754\n                     Local fix       0.89      0.03      0.06       266\n                Modules/Macros       0.96      0.87      0.91       326\n                       Problem       0.00      0.00      0.00        63\n               Problem summary       0.51      0.73      0.60       721\n           Related information       0.00      0.00      0.00        22\n         Resolving The Problem       0.00      0.00      0.00        60\n                 Temporary fix       0.00      0.00      0.00        32\n                  circumvenion       0.00      0.00      0.00       124\n                     component       0.00      0.00      0.00        49\n                 temporary_fix       0.00      0.00      0.00         2\n\n                     micro avg       0.64      0.64      0.64      3552\n                     macro avg       0.30      0.27      0.26      3552\n                  weighted avg       0.60      0.64      0.58      3552\n'}
result = {}
for i, v in enumerate(d["Logistic Regression"].splitlines()):
    if i == 0:
        continue
    val = v.strip().split("       ")
    if val[0]:
        result[val[0]] = " ".join(val[1:]).split()

for k, v in result.items():
    print(k)
    print(v)
weighted avg
[u'0.60', u'0.64', u'0.58', u'3552']
Local fix
[u'0.89', u'0.03', u'0.06', u'266']
Affected Products and Versions
[u'0.00', u'0.00', u'0.00', u'18']
component
[u'0.00', u'0.00', u'0.00', u'49']
Resolving The Problem
[u'0.00', u'0.00', u'0.00', u'60']
Error description
[u'0.48', u'0.56', u'0.52', u'754']
Problem summary
[u'0.51', u'0.73', u'0.60', u'721']
macro avg
[u'0.30', u'0.27', u'0.26', u'3552']
Related information
[u'0.00', u'0.00', u'0.00', u'22']
Applicable component levels
[u'0.96', u'0.85', u'0.90', u'241']
micro avg
[u'0.64', u'0.64', u'0.64', u'3552']
Answer
[u'0.00', u'0.00', u'0.00', u'30']
APAR Information
[u'0.74', u'1.00', u'0.85', u'844']
Problem
[u'0.00', u'0.00', u'0.00', u'63']
Modules/Macros
[u'0.96', u'0.87', u'0.91', u'326']
temporary_fix
[u'0.00', u'0.00', u'0.00', u'2']
circumvenion
[u'0.00', u'0.00', u'0.00', u'124']
Temporary fix
[u'0.00', u'0.00', u'0.00', u'32']
输出:

d = {'Logistic Regression': u'                                precision    recall  f1-score   support\n\n              APAR Information       0.74      1.00      0.85       844\nAffected Products and Versions       0.00      0.00      0.00        18\n                        Answer       0.00      0.00      0.00        30\n   Applicable component levels       0.96      0.85      0.90       241\n             Error description       0.48      0.56      0.52       754\n                     Local fix       0.89      0.03      0.06       266\n                Modules/Macros       0.96      0.87      0.91       326\n                       Problem       0.00      0.00      0.00        63\n               Problem summary       0.51      0.73      0.60       721\n           Related information       0.00      0.00      0.00        22\n         Resolving The Problem       0.00      0.00      0.00        60\n                 Temporary fix       0.00      0.00      0.00        32\n                  circumvenion       0.00      0.00      0.00       124\n                     component       0.00      0.00      0.00        49\n                 temporary_fix       0.00      0.00      0.00         2\n\n                     micro avg       0.64      0.64      0.64      3552\n                     macro avg       0.30      0.27      0.26      3552\n                  weighted avg       0.60      0.64      0.58      3552\n'}
result = {}
for i, v in enumerate(d["Logistic Regression"].splitlines()):
    if i == 0:
        continue
    val = v.strip().split("       ")
    if val[0]:
        result[val[0]] = " ".join(val[1:]).split()

for k, v in result.items():
    print(k)
    print(v)
weighted avg
[u'0.60', u'0.64', u'0.58', u'3552']
Local fix
[u'0.89', u'0.03', u'0.06', u'266']
Affected Products and Versions
[u'0.00', u'0.00', u'0.00', u'18']
component
[u'0.00', u'0.00', u'0.00', u'49']
Resolving The Problem
[u'0.00', u'0.00', u'0.00', u'60']
Error description
[u'0.48', u'0.56', u'0.52', u'754']
Problem summary
[u'0.51', u'0.73', u'0.60', u'721']
macro avg
[u'0.30', u'0.27', u'0.26', u'3552']
Related information
[u'0.00', u'0.00', u'0.00', u'22']
Applicable component levels
[u'0.96', u'0.85', u'0.90', u'241']
micro avg
[u'0.64', u'0.64', u'0.64', u'3552']
Answer
[u'0.00', u'0.00', u'0.00', u'30']
APAR Information
[u'0.74', u'1.00', u'0.85', u'844']
Problem
[u'0.00', u'0.00', u'0.00', u'63']
Modules/Macros
[u'0.96', u'0.87', u'0.91', u'326']
temporary_fix
[u'0.00', u'0.00', u'0.00', u'2']
circumvenion
[u'0.00', u'0.00', u'0.00', u'124']
Temporary fix
[u'0.00', u'0.00', u'0.00', u'32']

您可以使用第三方熊猫通过(“固定宽度格式化”)转换为数据帧。您的数据很混乱,可能需要编写逻辑来计算列宽或手动添加列宽。给定一个输入字典
d

from io import StringIO
import pandas as pd

df = pd.read_fwf(StringIO(d['Logistic Regression']), widths=[30, 11, 10, 10, 10])\
       .dropna().rename(columns={'Unnamed: 0': 'index'}).set_index('index')

print(df)

                                precision  recall  f1-score  support
index                                                               
APAR Information                     0.74    1.00      0.85    844.0
Affected Products and Versions       0.00    0.00      0.00     18.0
Answer                               0.00    0.00      0.00     30.0
Applicable component levels          0.96    0.85      0.90    241.0
Error description                    0.48    0.56      0.52    754.0
Local fix                            0.89    0.03      0.06    266.0
Modules/Macros                       0.96    0.87      0.91    326.0
Problem                              0.00    0.00      0.00     63.0
Problem summary                      0.51    0.73      0.60    721.0
Related information                  0.00    0.00      0.00     22.0
Resolving The Problem                0.00    0.00      0.00     60.0
Temporary fix                        0.00    0.00      0.00     32.0
circumvenion                         0.00    0.00      0.00    124.0
component                            0.00    0.00      0.00     49.0
temporary_fix                        0.00    0.00      0.00      2.0
micro avg                            0.64    0.64      0.64   3552.0
macro avg                            0.30    0.27      0.26   3552.0
weighted avg                         0.60    0.64      0.58   3552.0
然后使用字典理解:

res = {'Logistic Regression': {idx: df.loc[idx].tolist() for idx in df.index}}

print(res)

{'Logistic Regression':
 {'APAR Information': [0.74, 1.0, 0.85, 844.0],
  'Affected Products and Versions': [0.0, 0.0, 0.0, 18.0],
  'Answer': [0.0, 0.0, 0.0, 30.0],
  'Applicable component levels': [0.96, 0.85, 0.9, 241.0],
  'Error description': [0.48, 0.56, 0.52, 754.0],
  'Local fix': [0.89, 0.03, 0.06, 266.0],
  'Modules/Macros': [0.96, 0.87, 0.91, 326.0],
  'Problem': [0.0, 0.0, 0.0, 63.0],
  'Problem summary': [0.51, 0.73, 0.6, 721.0],
  'Related information': [0.0, 0.0, 0.0, 22.0],
  'Resolving The Problem': [0.0, 0.0, 0.0, 60.0],
  'Temporary fix': [0.0, 0.0, 0.0, 32.0],
  'circumvenion': [0.0, 0.0, 0.0, 124.0],
  'component': [0.0, 0.0, 0.0, 49.0],
  'macro avg': [0.3, 0.27, 0.26, 3552.0],
  'micro avg': [0.64, 0.64, 0.64, 3552.0],
  'temporary_fix': [0.0, 0.0, 0.0, 2.0],
  'weighted avg': [0.6, 0.64, 0.58, 3552.0]}}

你最初的“dict”看起来不像字典。也许你可以用熊猫?