【发布时间】:2015-01-28 18:47:33
【问题描述】:
我有一个 JSON 文件,我正在将其作为字典读取。我所拥有的是这样的:
"20101021": {
"4x4": {
"Central Spectrum": 5,
"Full Frame": 5,
"Custom": 1
},
"4x2": {
"Central Spectrum": 5,
"Full Frame": 5
},
"1x1": {
"Central Spectrum": 5,
"Full Frame": 4
},
},
"20101004": {
"4x4": {
"Central Spectrum": 5,
"Full Frame": 5
},
"4x2": {
"Central Spectrum": 5,
"Full Frame": 5
},
"1x1": {
"Central Spectrum": 5,
"Full Frame": 5
}
等等。
我正在尝试计算1x1、4x2(等)和Central Spectrum 和Full Frame 的所有组合的总和(在所有日期),在这个例子中我想加起来5s .
到目前为止我所拥有的是这个(使用itertools 和Counter()):
bins = map("x".join, itertools.product('124', repeat=2))
rois = ['Full Frame', 'Central Spectrum']
types = itertools.product(bins, rois)
c = collections.Counter(dict)
for type in types:
print "%s : %d" % (type, c[type])
这会打印出所有组合的漂亮列表,但无法对值进行任何实际求和。你能帮忙吗?
【问题讨论】:
标签: python json dictionary nested itertools