gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 00:10:23 25 4
gpt4 key购买 nike

我有一个字典:

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com