Python-将字典转换为嵌套字典

时间:2018-10-05 10:03:34

标签: python dictionary

我有一个 dict

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

                                precision    recall  f1-score   support

              APAR Information       0.74      1.00      0.85       844
Affected Products and Versions       0.00      0.00      0.00        18
                        Answer       0.00      0.00      0.00        30
   Applicable component levels       0.96      0.85      0.90       241
             Error description       0.48      0.56      0.52       754
                     Local fix       0.89      0.03      0.06       266
                Modules/Macros       0.96      0.87      0.91       326
                       Problem       0.00      0.00      0.00        63
               Problem summary       0.51      0.73      0.60       721
           Related information       0.00      0.00      0.00        22
         Resolving The Problem       0.00      0.00      0.00        60
                 Temporary fix       0.00      0.00      0.00        32
                  circumvenion       0.00      0.00      0.00       124
                     component       0.00      0.00      0.00        49
                 temporary_fix       0.00      0.00      0.00         2

                     micro avg       0.64      0.64      0.64      3552
                     macro avg       0.30      0.27      0.26      3552
                  weighted avg       0.60      0.64      0.58      3552

我想将此字典转换为嵌套字典,例如,

{'Logistic Regression':
{'APAR Information':'0.74','1.00','0.85','844'},
{'Affected Products and Versions':'0.00','0.00','0.00','18'}
.
.
.}

如何实现?可以通过dict内置函数完成吗?

2 个答案:

答案 0 :(得分:2)

这是一种方法。

演示:

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

答案 1 :(得分:0)

您可以使用第三方熊猫通过pd.read_fwf(“固定宽度格式”)转换为数据框。您的数据混乱,您可能需要编写逻辑来计算列宽或手动添加它们。给定输入字典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]}}