在python中求和嵌套字典值

编程入门 行业动态 更新时间:2024-10-23 15:32:53
本文介绍了在python中求和嵌套字典值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有这样的字典,

data = {11L:[{'a':2,'b' :1},{'a':2,'b':3}], 22L:[{'a':3,'b':2},{'a':2,'b' :5},{'a':4,'b':2},{'a':1,'b':5},{'a':1,'b':0}], 33L:{'a':1,'b':2},{'a':3,'b':5},{'a':5,'b':2},{'a' :1,'b':3},{'a':1,'b':6},{'a':2,'b':0}], 44L:[{'a' :4,'b':2},{'a':4,'b':5},{'a':3,'b':1},{'a':3,'b' },{'a':2,'b':3},{'a':1,'b':2},{'a':1,'b':0}]}

这里我将摆脱外键,并给出新的键值1,2,3等等,我想得到如下所示的结果,

result = {1:{'a':10,'b':7} 2:{ 'A':11, 'b':18},3:{ 'A':12, 'b':5},4:{ '一个':5, 'b':11},5: {'a':3,'b':9},6:{'a':3,'b':2},7:{'a':1,'b':0}}

我尝试过这样的一些东西,但是我得到所需的结果,

d = defaultdict(int) for dct in data.values(): for k,v in dct.items():d [k] + = v print dict(d)

我希望结果字典的键是动态的,就像上面的数据字典一样, 7个键值对,因此我们有结果字典有7个键等等

解决方案

你想在这里使用一个列表,并且您想要使用 Counter()对象来使求和更容易:

from collections import counter from itertools import izip_longest fordcts在data.values()中:为i,dct in枚举(dcts):如果我> = len(结果): result.append(Counter(dct)) else: result [i] .update(dct)

结果:

>>>结果 [Counter({'a':10,'b':7}),Counter({'b':18,'a':11}),Counter({'a':12, b':5}),Counter({'b':11,'a':5}),Counter({'b':9,'a':4}),Counter({'a' 'b':2}),Counter({'a':1,'b':0})]

Counter()对象是 dict 的子类,所以它们以其他方式表现为字典。如果您有以 dict 值之后添加以下行:

result = [dict(r)for r in result]

灵感来自Eric,您可以将以上内容转换为单行:

从集合import counter from itertools import izip_longest result = [sum(map,col),Counter()) for izip_longest中的col(* data.values(),fillvalue = {})]

此版本与上述循环略有不同,该键在求和时从计数器中删除0。如果您想在上一个计数器中保留'b':0 ,请使用:

[reduce(lambda c,d:c.update(d)或c,col,Counter()) for izip_longest(* data.values(),fillvalue = {})]

这再次使用 .update() / p>

I have a dictionary like this,

data={11L: [{'a': 2, 'b': 1},{'a': 2, 'b': 3}], 22L: [{'a': 3, 'b': 2},{'a': 2, 'b': 5},{'a': 4, 'b': 2},{'a': 1, 'b': 5}, {'a': 1, 'b': 0}], 33L: [{'a': 1, 'b': 2},{'a': 3, 'b': 5},{'a': 5, 'b': 2},{'a': 1, 'b': 3}, {'a': 1, 'b': 6},{'a':2,'b':0}], 44L: [{'a': 4, 'b': 2},{'a': 4, 'b': 5},{'a': 3, 'b': 1},{'a': 3, 'b': 3}, {'a': 2, 'b': 3},{'a':1,'b':2},{'a': 1, 'b': 0}]}

Here i ll get rid of the outer keys, and give new key values 1, 2 , 3 so on, i want to get the result as shown below,

result={1:{'a':10,'b':7},2:{'a':11,'b':18},3:{'a':12,'b':5},4:{'a':5,'b':11},5:{'a':3,'b':9},6:{'a':3,'b':2},7:{'a':1,'b':0}}

I tried some thing like this, but i dint get the required result,

d = defaultdict(int) for dct in data.values(): for k,v in dct.items(): d[k] += v print dict(d)

I want the keys of result dictionary to be dynamic, like in the above data dictionary we have 44 which has highest with 7 key value pairs, hence we have the result dictionary with 7 keys and so on

解决方案

You want to use a list here, and you want to perhaps use Counter() objects to make the summing that much easier:

from collections import Counter from itertools import izip_longest for dcts in data.values(): for i, dct in enumerate(dcts): if i >= len(result): result.append(Counter(dct)) else: result[i].update(dct)

Result:

>>> result [Counter({'a': 10, 'b': 7}), Counter({'b': 18, 'a': 11}), Counter({'a': 12, 'b': 5}), Counter({'b': 11, 'a': 5}), Counter({'b': 9, 'a': 4}), Counter({'a': 3, 'b': 2}), Counter({'a': 1, 'b': 0})]

Counter() objects are subclasses of dict, so they otherwise behave as dictionaries. If you have to have dict values afterwards, add the following line:

result = [dict(r) for r in result]

Taking inspiration from Eric, you can transform the above into a one-liner:

from collections import Counter from itertools import izip_longest result = [sum(map(Counter, col), Counter()) for col in izip_longest(*data.values(), fillvalue={})]

This version differs slightly from the loop above in that keys that are 0 are dropped from the counter when summing. If you want to keep 'b': 0 in the last counter, use:

[reduce(lambda c, d: c.update(d) or c, col, Counter()) for col in izip_longest(*data.values(), fillvalue={})]

This uses .update() again.

更多推荐

在python中求和嵌套字典值

本文发布于:2023-11-12 03:46:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580422.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   字典   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!