使用两个键作为索引对字典值求和:如何实现? [英] Sum dictionary values, using two keys as index: how to achieve this?
问题描述
我有以下字典:
res = [{'name': 'mfi', 'percentage': 100.0, 'tax_base': 1000.0, 'tax_amount': 140.0},
{'name': 'serv', 'percentage': 100.0, 'tax_base': 1000.0, 'tax_amount': 140.0},
{'name': 'inv', 'percentage': 100.0, 'tax_base': 1200.0, 'tax_amount': 168.0},
{'name': 'mfi', 'percentage': 50.0, 'tax_base': 1500.0, 'tax_amount': 210.0},
{'name': 'none', 'percentage': 0.0, 'tax_base': 1000.0, 'tax_amount': 0.0},
{'name': 'none', 'percentage': 0.0, 'tax_base': 900.0, 'tax_amount': 126.0},
{'name': 'mfi', 'percentage': 50.0, 'tax_base': 1000.0, 'tax_amount': 140.0}]
在这本词典中,我需要对"tax_base"和"tax_amount"值键进行求和,并使用键"name"和"percentage"作为索引.
From this dictionary, I need to sum 'tax_base' and 'tax_amount' value keys, and use keys 'name' and 'percentage' as index.
因此,我需要:
res_final = [{'name': 'mfi', 'percentage': 100.0, 'tax_base': 1000.0, 'tax_amount': 140.0},
{'name': 'mfi', 'percentage': 50.0, 'tax_base': 2500.0, 'tax_amount': 350.0},
{'name': 'serv', 'percentage': 100.0, 'tax_base': 1000.0, 'tax_amount': 140.0},
{'name': 'inv', 'percentage': 100.0, 'tax_base': 1200.0, 'tax_amount': 168.0},
{'name': 'none', 'percentage': 0.0, 'tax_base': 1900.0, 'tax_amount': 126.0},
]
我该如何实现?能给我样品吗?
How can I achieve this? Can you provide me a sample please?
推荐答案
采用原始输入 res
和首选输出 res_final
,以下代码将起作用:
Taking your original input, res
and your preferred output res_final
, the following code would work:
# Creating a temporary dictionary with the aggregation
temp_result = {}
for each in res:
key = (each['name'], each['percentage'])
if key not in temp_result:
temp_result[key] = dict(tax_base=0, tax_amount=0)
temp_result[key]['tax_base'] += each['tax_base']
temp_result[key]['tax_amount'] += each['tax_amount']
# Transforming the temporary dictionary to the correct format
final_result = []
for (name, percentage), taxes in temp_result.items():
final_result.append(dict(
name=name,
percentage=percentage,
tax_base=taxes['tax_base'],
tax_amount=taxes['tax_amount']
))
for each in final_result:
print(each)
结果将是:
{'name': 'mfi', 'percentage': 100.0, 'tax_base': 1000.0, 'tax_amount': 140.0}
{'name': 'serv', 'percentage': 100.0, 'tax_base': 1000.0, 'tax_amount': 140.0}
{'name': 'inv', 'percentage': 100.0, 'tax_base': 1200.0, 'tax_amount': 168.0}
{'name': 'mfi', 'percentage': 50.0, 'tax_base': 2500.0, 'tax_amount': 350.0}
{'name': 'none', 'percentage': 0.0, 'tax_base': 1900.0, 'tax_amount': 126.0}
说明
在第一部分中,我们创建一个新的词典,该词典以 name
和 percentage
的组合作为 tuple
,并以使用该键的 tax_base
和 tax_amount
来计算字典.
Explanation
In the first part we create a new dictionary, that has as key the combination of name
and percentage
as a tuple
, and as value a dictionary with the tax_base
and tax_amount
for that key.
然后,我们检查密钥是否已在我们的字典中,如果不是,则创建密钥.最后一步是对 tax_base
和 tax_amount
进行求和.
Then we check if the key is already in our dictionary and if it isn't we create the key. The final step is summing the tax_base
and tax_amount
.
现在,我们有一本包含所有信息的词典,但是格式不正确.第二部分解决了这一问题.我们将密钥再次拆分为 name
和 percentage
,然后将数据与 tax_base
和 tax_amount
合并为一个字典.
Now we have one dictionary with all the information, but not in the right format. The second part takes care of that. We split the key again into the name
and percentage
and merge the data with tax_base
and tax_amount
to one dict.
以防人们想知道如何使用 pd.DataFrame
.
In case people are wondering how to do it with pd.DataFrame
.
import pandas as pd
df = pd.DataFrame(res)
res = df.groupby(['name', 'percentage'], as_index=False).sum()
final_result = res.to_dict('records')
for each in final_result:
print(each)
将产生相同的输出,但不能保证与输入的顺序相同.
Will result in the same output, but it is not guaranteed to be in the same order as the input.
这篇关于使用两个键作为索引对字典值求和:如何实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!