Skip to main content
 首页 » 编程设计

python之嵌套字典Python中的分组数据

2025年05月04日3698°冷暖

我有这样一本字典,

data = {'04-01-2012': [{1: 0.93}, {2: 0.9195000000000001}, {3: 0.9090000000000001}, {4: 0.8985000000000002}, 
                       {5: 0.8880000000000002}, {6: 0.8775000000000003}, {7: 0.8670000000000003}, 
                       {8: 0.8565000000000004}, {9: 0.8460000000000004}], 
        '12-01-2012': [{1: 0.96}], 
        '07-01-2012': [{1: 0.96}, {2: 0.95}, {3: 0.94}, {4: 0.9299999999999999}, {5: 0.9199999999999999}, 
                       {6: 0.9099999999999999}], 
        '06-01-2012': [{1: 0.945}, {2: 0.9365}, {3: 0.928}, {4: 0.9195000000000001}, {5: 0.9110000000000001}, 
                       {6: 0.9025000000000002}, {7: 0.8940000000000002}], 
        '10-01-2012': [{1: 0.93}, {2: 0.9244}, {3: 0.9188}], 
        '05-01-2012': [{1: 0.935}, {2: 0.926}, {3: 0.917}, {4: 0.908}, {5: 0.899}, {6: 0.89}, {7: 0.881}, {8: 0.872}], 
        '11-01-2012': [{1: 0.945}, {2: 0.9325}], 
        '02-01-2012': [{1: 0.94}, {2: 0.9299999999999999}, {3: 0.9199999999999999}, {4: 0.9099999999999999}, 
                       {5: 0.8999999999999999}, {6: 0.8899999999999999}, {7: 0.8799999999999999}, 
                       {8: 0.8699999999999999}, {9: 0.8599999999999999}, {10: 0.8499999999999999}, 
                       {11: 0.8399999999999999}], 
        '03-01-2012': [{1: 0.955}, {2: 0.9455}, {3: 0.936}, {4: 0.9265000000000001}, {5: 0.9170000000000001}, 
                       {6: 0.9075000000000002}, {7: 0.8980000000000002}, {8: 0.8885000000000003}, 
                       {9: 0.8790000000000003}, {10: 0.8695000000000004}], 
        '08-01-2012': [{1: 0.94}, {2: 0.9295}, {3: 0.919}, {4: 0.9085000000000001}, {5: 0.8980000000000001}], 
        '01-01-2012': [{1: 0.95}, {2: 0.94}, {3: 0.9299999999999999}, {4: 0.9199999999999999}, {5: 0.9099999999999999}, 
                       {6: 0.8999999999999999}, {7: 0.8899999999999999}, {8: 0.8799999999999999}, 
                       {9: 0.8699999999999999}, {10: 0.8599999999999999}, {11: 0.8499999999999999}, 
                       {12: 0.8399999999999999}], 
        '09-01-2012': [{1: 0.92}, {2: 0.91}, {3: 0.9}, {4: 0.89}]} 

我需要遍历字典值并将所有 1、2 等分组。

到目前为止,这是我的代码

from collections import defaultdict 
 
final = defaultdict(list) 
 
for k, v in data.items(): 
    new_data = next(iter(v)) 
    for m, n in new_data.items(): 
        final[m].append(n) 
 
print(final) 
 
# defaultdict(<class 'list'>, {1: [0.935, 0.92, 0.955, 0.96, 0.94, 0.93, 0.95, 0.96, 0.945, 0.94, 0.945, 0.93]}) 

它只对所有 1 进行分组,不对 2 进行分组,依此类推。我做错了什么?

请您参考如下方法:

你忘了遍历许多小字典:

from collections import defaultdict 
 
final = defaultdict(list) 
 
for k, v in data.items(): 
    for d in v:                 # <-- this was missing 
        for m, n in d.items(): 
            final[m].append(n) 
 
print(final) 

(您只调用了 next(...),它只产生第一个项目。)

输出:

defaultdict(, {1: [0.96, 0.935, 0.93, 0.945, 0.96, 0.95, 0.93, 0.94, 0.945, 0.955, 0.94, 0.92], 2: [0.926, 0.9244, 0.9365, 0.95, 0.94, 0.9195000000000001, 0.9299999999999999, 0.9325, 0.9455, 0.9295, 0.91], 3: [0.917, 0.9188, 0.928, 0.94, 0.9299999999999999, 0.9090000000000001, 0.9199999999999999, 0.936, 0.919, 0.9], 4: [0.908, 0.9195000000000001, 0.9299999999999999, 0.9199999999999999, 0.8985000000000002, 0.9099999999999999, 0.9265000000000001, 0.9085000000000001, 0.89], 5: [0.899, 0.9110000000000001, 0.9199999999999999, 0.9099999999999999, 0.8880000000000002, 0.8999999999999999, 0.9170000000000001, 0.8980000000000001], 6: [0.89, 0.9025000000000002, 0.9099999999999999, 0.8999999999999999, 0.8775000000000003, 0.8899999999999999, 0.9075000000000002], 7: [0.881, 0.8940000000000002, 0.8899999999999999, 0.8670000000000003, 0.8799999999999999, 0.8980000000000002], 8: [0.872, 0.8799999999999999, 0.8565000000000004, 0.8699999999999999, 0.8885000000000003], 9: [0.8699999999999999, 0.8460000000000004, 0.8599999999999999, 0.8790000000000003], 10: [0.8599999999999999, 0.8499999999999999, 0.8695000000000004], 11: [0.8499999999999999, 0.8399999999999999], 12: [0.8399999999999999]})