汇总嵌套字典条目 [英] summing nested dictionary entries

查看:74
本文介绍了汇总嵌套字典条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON文件,正在以字典的形式读取.我拥有的是这样的:

I have a JSON file that I'm reading in as a dictionary. What I have is something like:

        "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
            }

,依此类推. 我正在尝试计算1x14x2(等)和Central SpectrumFull Frame的所有组合的总和(所有日期),在本示例中,我想将5加起来

and so on. I am trying to calculate sums (over all dates) for all combinations of 1x1, 4x2 (etc.) and Central Spectrum and Full Frame, in this example I'd want to add up the 5s.

到目前为止,我的情况是这样(使用itertoolsCounter()):

What I have so far is this (using itertools and 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])

这将打印出所有组合的完整列表,但无法进行任何实际的值求和.你能帮忙吗?

This prints out a nice list of all combinations, but fails to do any actual summing of values. Can you help?

推荐答案

也许我误解了预期的最终结果,但是您可能不需要计数器...如果您只知道要去,简单的sum就足够了具有两个嵌套级别.

Maybe I misunderstood the expected final result, but you might not need counters... A simple sum could suffice if you know that you're only going to have two levels of nesting.

假设您将字典的json字典加载到名为data的变量中.

Let's assume you loaded your json dictionary of dictionaries into a variable called data.

然后您可以执行以下操作:

Then you can do:

results = {}
for key in data.keys():
    # key is '20101021', '20101004'...
    # data[key].keys() is '4x4, '4x2'... so let's make sure
    # that the result dictionary contains all those '4x4', '4x2'
    # being zero if nothing better can be calculated.
    results[key] = dict.fromkeys(data[key].keys(), 0)

    for sub_key in data[key].keys():
        # sub_key is '4x4', '4x2'...
        # Also, don't consider a 'valid value' someting that is not a
        # "Central Spectrum" or a "Full Frame"
        valid_values = [
            int(v) for k, v in data[key][sub_key].items()
            if k in ["Central Spectrum", "Full Frame"]
        ]
        # Now add the 'valid_values'
        results[key][sub_key] = sum(valid_values)
print results

哪个输出:

{
  u'20101021': {u'1x1': 9, u'4x4': 10, u'4x2': 10},
  u'20101004': {u'1x1': 10, u'4x4': 10, u'4x2': 10}
}

在许多情况下,我只使用 dict.keys(),因为这也许可以澄清过程? (好吧,一旦 dict.items()),您还具有 dict.values()(所有树函数都有等效的迭代器),这可能会缩短您的代码.另外,请查看 dict.fromkeys 的作用.

In many cases, I only used dict.keys() because maybe that clarifies the process? (well, and once dict.items()) You also have dict.values() (and all the tree functions have their iterator equivalents) which might shorten your code. Also, see what dict.fromkeys does.

如果您希望随时间添加(或收集")数据,则需要将results[key]从日期字符串(如答案中上面所示)移动到1x14x4. ..

If you want data added (or "collected") over time, then you need to need to move your results[key] from the date string (as shown above in the answer) to the 1x1, 4x4...

VALID_KEYS = ["Central Spectrum", "Full Frame"]
results = {}
for key_1 in data.keys():
    # key_1 is '20101021', '20101004'...

    for key_2 in data[key_1].keys():
        # key_2 is '4x4', '4x2'...
        if key_2 not in results:
            results[key_2] = dict.fromkeys(VALID_KEYS, 0)
        for key_3 in data[key_1][key_2].keys():
            # key_3 is 'Central Spectrum', 'Full Frame', 'Custom'...
            if key_3 in VALID_KEYS:
                results[key_2][key_3] += data[key_1][key_2][key_3]
print results

哪个输出:

{
    u'1x1': {'Central Spectrum': 10, 'Full Frame': 9},
    u'4x4': {'Central Spectrum': 10, 'Full Frame': 10},
    u'4x2': {'Central Spectrum': 10, 'Full Frame': 10}
}

这篇关于汇总嵌套字典条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆