列表字典去重求和
- 问题
a = [{"adcode": 200, "count": 30}, {"adcode": 200, "count": 5}]
结果:
[{"adcode": 200, "count": 35}]
- 解决思路
- 使用pandas DataFrame对象来进行计算
from pandas import DataFrame
data = DataFrame(a)
ret = data.groupby(['adcode'])['count'].sum()
result = [{'adcode': i, 'count': j} for i, j in ret.items()]