[英]Python list: Sum values on list and remove duplicates
I have the following list in Python:我在 Python 中有以下列表:
my_list = [{'code_id': 'A', 'amount': 100.0},{'code_id': 'B', 'amount': 150.0},{'code_id': 'C', 'amount': 200.0},{'code_id': 'A', 'amount': 120.0},{'code_id': 'B', 'amount': 300.0},{'code_id': 'D', 'amount': 180.0}]
From the list above, I need to create a new list with no duplicates;从上面的列表中,我需要创建一个没有重复的新列表; but sum the "amount" for all items in the above list.但将上面列表中所有项目的“数量”相加。
I need to achieve a final list like:我需要实现最终列表,例如:
final_list = [{'code_id': 'A', 'amount': 220.0},{'code_id': 'B', 'amount': 450.0},{'code_id': 'C', 'amount': 200.0},{'code_id': 'D', 'amount': 180.0}]
I was able to remove duplicates but do not know how to sum values in the process.我能够删除重复项,但不知道如何在此过程中对值求和。 The sample code I have used:我使用的示例代码:
final_list = []
seen = set()
for dic in my_list:
key = (dic['code_id'])
if key in seen:
continue
final_list.append(dic)
seen.add(key)
How can I achieve this in Python?我怎样才能在 Python 中实现这一点?
I would start with a temporary defaultdict
that keeps track of the 'amount'
for each 'code_id'
:我将从一个临时defaultdict
开始,它跟踪每个'code_id'
'amount'
from collections import defaultdict
my_list = [{'code_id': 'A', 'amount': 100.0},{'code_id': 'B', 'amount': 150.0},{'code_id': 'C', 'amount': 200.0}, {'code_id': 'A', 'amount': 120.0},{'code_id': 'B', 'amount': 300.0},{'code_id': 'D', 'amount': 180.0}]
tmp = defaultdict(int)
for d in my_list:
tmp[d['code_id']] += d['amount']
# if tmp was a normal dict, you could use
# tmp[d['code_id']] = tmp.get(d['code_id'], 0) + d['amount']
print(tmp)
# defaultdict(int, {'A': 220.0, 'B': 450.0, 'C': 200.0, 'D': 180.0})
... and then transform the structure of tmp
to arrive at the desired result ...然后转换tmp
的结构以达到所需的结果
result = [{'code_id': k, 'amount': v} for k, v in tmp.items()]
print(result)
# [{'code_id': 'A', 'amount': 220.0}, {'code_id': 'B', 'amount': 450.0}, {'code_id': 'C', 'amount': 200.0}, {'code_id': 'D', 'amount': 180.0}]
For the pandas
users out there:对于pandas
用户:
>>> pd.DataFrame(my_list).groupby('code_id', as_index=False).sum().to_dict(orient='records')
[{'code_id': 'A', 'amount': 220.0},
{'code_id': 'B', 'amount': 450.0},
{'code_id': 'C', 'amount': 200.0},
{'code_id': 'D', 'amount': 180.0}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.