我有这样的字典,
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&#39> 等组合在一起。
这是我目前的代码
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个进行分组,依此类推。我做错了什么?
答案 0 :(得分:5)
你忘了迭代许多小词典:
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,9.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.919999999999999999,0.936,0.919,0.9],4:[0.908,0.9195000000000001,0.929999999999999999,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.84600000 00000004,0.8599999999999999,0.8790000000000003],10:[0.859999999999999999,0.8499999999999999,0.8695000000000004],11:[0.849999999999999999,0.8399999999999999],12:[0.8399999999999999]])
答案 1 :(得分:0)
new_data = next(iter(v))`
这是出错的地方。这只返回你的iterable中的下一个值,在这种情况下是绑定到key的键值对:1。
你可以通过添加一个print语句来看到这个:
from collections import defaultdict
final = defaultdict(list)
print final
for k, v in data.items():
new_data = next(iter(v))
for m, n in new_data.items():
print new_data
final[m].append(n)
#{1: 0.96}
#{1: 0.935}
#{1: 0.93}
#{1: 0.945}
#{1: 0.96}
#{1: 0.95}
#{1: 0.93}
#{1: 0.94}
#{1: 0.945}
#{1: 0.955}
#{1: 0.94}
#{1: 0.92}
然后解决方案是实际抓取所有项目,而不仅仅是第一项,这可以通过很多方式完成,其中最简单,最丑陋的是嵌套另一个循环。
from collections import defaultdict
final = defaultdict(list)
for k, v in data.items():
iterable = iter(v)
for i in range(len(v)):
new_data = iterable.next()
for m, n in new_data.items():
final[m].append(n)
print(final)