我有一个词典列表
dictlist = [{0:1,1:1,2:1,3:1}, {0:1,1:0,2:1,3:0}, {0:1,1:0,2:0,3:1}]
我需要输出为dict,其中每个键的值是单个dicts中该键的值列表。
dict_needed = {0:[1,1,1],1:[1,0,0],2:[1,1,0],3:[1,0,1]}
我该怎么做?
答案 0 :(得分:3)
from collections import defaultdict
dict_needed = defaultdict(list)
for d in dictlist:
for k, v in d.iteritems():
dict_needed[k].append(v)