如何使用权重将字典合并在一起?
d1 = {'weight':1, 'data': { 'apples': 8, 'oranges': 7 } }
d2 = {'weight':3, 'data': { 'apples': 4, 'bananas': 3 } }
all_dictionaries = [d1, d2, ... ]
def mergeDictionariesWithWeight(all_dictionaries)
如何将这些字典合并在一起(如果重叠,则多个值与权重)
该函数将返回:
{ 'apples': 4, 'oranges': 7, 'bananas': 3 }
Apples is 4
因为 8 * .25 + 4 * .75
编辑:我刚刚写了一篇取平均值的文章,类似这样。但当然,这与我想要做的确实不同,因为我将所有内容都放在列表中,然后除以长度。
result = {}
keymap = {}
for the_dict in dlist:
for (k, v) in the_dict.items():
if not keymap.has_key(k):
keymap[k] = []
keymap[k].append(v)
for (k, v) in keymap.items():
average = sum(int(x) for x in keymap[k]) / float(len(keymap[k]))
result[k] = float(average)
return result
d1 = {'weight':1, 'data': { 'apples': 8, 'oranges': 7 } }
d2 = {'weight':3, 'data': { 'apples': 4, 'bananas': 3 } }
all_dictionaries = [d1, d2, ... ]
def mergeDictionariesWithWeight(all_dictionaries)
How do I merge these dictionaries together (if overlap, multiple value with the weight)
The function would return:
{ 'apples': 4, 'oranges': 7, 'bananas': 3 }
Apples is 4
because 8 * .25 + 4 * .75
Edit: I just wrote one that takes the average, something like this. But of course it's really different from what I want to do, because I stick everything in a list and just divide by the length.
result = {}
keymap = {}
for the_dict in dlist:
for (k, v) in the_dict.items():
if not keymap.has_key(k):
keymap[k] = []
keymap[k].append(v)
for (k, v) in keymap.items():
average = sum(int(x) for x in keymap[k]) / float(len(keymap[k]))
result[k] = float(average)
return result
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果需要浮点结果
关于defaultdict的注释
通常您会看到
defaultdict(int)
或defaultdict(list)
甚至可能是defaultdict(set)
。 defaultdict 的参数必须可以不带参数地调用。每当发现密钥丢失时,都会使用调用此参数的结果。 的默认值,即 - 调用此函数会返回字典
这通常用于计数,因为
int()
返回 0。如果您希望默认值为1 而不是 0,它更棘手,因为您无法将参数传递给 int,但您所需要的只是一个返回 1 的可调用函数。通过使用 lambda 函数。在这个答案中,我想跟踪加权总和以及权重总和。我可以通过使用二元组作为默认值来做到这一点。
If you need floating point result
Notes about defaultdict
Often you see
defaultdict(int)
ordefaultdict(list)
maybe evendefaultdict(set)
. The argument to defaultdict must be callable with no parameters. The result of calling this parameter is used whenever a key is found to be missing. ie - calling this returns the default value for the dictionaryfor example
This is often used for counting things up because
int()
returns 0. If you want the default value to be 1 instead of 0, it's more tricky because you can't pass a parameter to int, but all you need is a callable that returns 1. This can be accomplished without too much fuss by using a lambda function.In this answer, I want to keep track of the weighted total, and the total of the weights. I can do this by using a 2-tuple as the default value.
这是一个首先使用临时字典将项目收集到列表中的解决方案,然后计算最终的加权字典。可能不需要临时就可以完成,但这很容易理解。
返回:{'苹果':5.0,'橙子':7,'香蕉':3}(因为 8 * .25 + 4 * .75 = 5.0)
Here's a solution that first uses gathers the items into a list using a temporary dict, and then computes the final weighted dict. It can probably be done without a temporary, but this is easy to understand.
Returns: {'apples': 5.0, 'oranges': 7, 'bananas': 3} (because 8 * .25 + 4 * .75 = 5.0)
试试这个:
try this:
在算法上与gnibbler的无法区分,但不知何故生成器表达式让我很高兴。
Algorithmically indistinguishable from gnibbler's, but somehow the generator expression pleases me.