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

标签 python dictionary

我有一个字典:

{'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 内置函数来完成吗?

最佳答案

这是一种方法。

演示:

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

关于Python-将字典转换为嵌套字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52663119/

相关文章:

python-3.x - 无法将网络抓取输出作为字典返回

ios - 如何在计算属性上设置值

c++ - C++ OpenCV 2.3 中缺少 MoveWindow()

Python for 循环和字典索引未按预期工作

python - 如何使用带有 matplotlib 的 pandas 来创建 3D 图

javascript - memoize 函数可以使用 ES6 Map 还是哈希表?

c# - 元组(或数组)作为 C# 中的字典键

python - "class MyDict(dict): pass"的用途是什么,它与普通字典有什么不同?

python - 无法在 IntelliJ IDEA 中创建新的 GAE (Python) 项目

python - pcolormesh 绘制的不是点而是数据点之间的线