简体   繁体   English

对一个元组和一个嵌套字典求和

[英]Summing a tuple-of-tuples and a nested dict

I have data that can be represented in two different forms (for historical reasons that I won't go into). 我有可以两种不同形式表示的数据(出于历史原因,我不再赘述)。 The first is a tuple of tuple s: 第一个是tupletuple

t = (('a', 'x', 3), 
('a', 'f', 1), 
('b', 'r', 23), 
('b', 'e', 3))

And the second as a dict of dict s: 第二个是dictdict

d = {'a' : {'x': 45, 'f' : 4},
     'b' : {'r' : 34, 'e' : 45}}

Same data, different representation. 相同的数据,不同的表示形式。 I now need to end up with a combination of the two (and must maintain the tuple-of-tuples form rather than the nested dict form), with the values summed . 现在,我需要结束两者的组合(并且必须保持元组的元组形式,而不是嵌套的dict形式),其值必须相加 ie

(('a', 'x', 48), 
('a', 'f', 5), 
('b', 'r', 57), 
('b', 'e', 48))

It seems this is a two-step process (convert the nested dict to a tuple of tuples, then sum the corresponding tuples within each tuple). 看来这是一个两步过程(将嵌套的dict转换为元组的元组,然后将每个元组中的相应元组相加)。 I'm struggling to get past the first part, I am missing two tuples (and I don't like how I have hardcoded the indexing either): 我正在努力超越第一部分,我错过了两个元组(我也不喜欢对索引进行硬编码的方式):

In [1025]: def f(d):
    for k, v in d.items():
        yield (k, d[k].keys()[0], d[k].values()[0])
   ......:         

In [1026]: for i in f(d):

    print i
   ......:     
('a', 'x', 45)
('b', 'r', 34)

What is a better way? 有什么更好的方法?

You can use a generator expression within tuple() , by looping over your tuples and summing the third item with it's relative value in dictionary: 您可以在tuple()使用生成器表达式,方法是遍历元组,并将第三项与其在字典中的相对值相加:

>>> tuple((i, j, k + d.get(i, {}).get(j, 0)) for i, j, k in t)
(('a', 'x', 48),
 ('a', 'f', 5),
 ('b', 'r', 57),
 ('b', 'e', 48))

Note that the advantage of using dict.get() method is that it returns 0 if the key doesn't exist in dictionary. 请注意,使用dict.get()方法的好处是,如果字典中不存在该键,它将返回0。

Note that if it doesn't make any difference for you to have a list of tuples or tuple of tuples, you can use a list comprehension instead of a generator expression. 请注意,如果拥有元组列表或元组元组对您没有任何影响,则可以使用列表推导而不是生成器表达式。 because list comprehension is more optimized in terms of runtime as it doesn't need to call extra methods like next() in a generator function in order to retrieve the items. 因为列表理解在运行时方面得到了更好的优化,因为它不需要在生成器函数中调用诸如next()类的额外方法来检索项目。

You can use a list comprehension converted to a tuple provided you are sure that all your tuple and dicts contain exactly same elements (it works with current example): 您可以使用转换为元组的列表推导,前提是您确保所有元组和字典都包含完全相同的元素(适用于当前示例):

tuple([(x, y, z + d[x][y]) for x, y, z in t ])

correctly gives: 正确给出:

(('a', 'x', 48), ('a', 'f', 5), ('b', 'r', 57), ('b', 'e', 48))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM