python - 字典中项目的累积总和
问题描述
我有一个这样的字典列表:
X = [{"t":1, "a":1, "b":3},
{"t":2, "a":2, "b":4}]
如何得到:
[{"t":1, "a":1, "b":3}, {"t":2, "a":3, "b":7}]
在所需输出的第二个元素中, 的值"b"
是7
,它是到该点的值的累积总和,"b"
对于其他键也是如此。
我知道我可以通过 pandas 做到这一点。但是有没有更pythonic的解决方案?
解决方案
collections.Counter
您可以使用对象以您想要累积的方式更新的事实:
import collections
def cumulative_elementwise_sum(ds):
result = collections.Counter()
for d in ds:
result.update(d)
yield dict(result)
当遇到新密钥时,这也将“做正确的事”。例子:
>>> x = [
... {'t': 1, 'a': 1, 'b': 3},
... {'t': 2, 'a': 2, 'b': 4},
... {'t': 1, 'a': 4, 'b': 1, 'd': 2},
... ]
>>> list(cumulative_elementwise_sum(x))
[{'t': 1, 'a': 1, 'b': 3},
{'t': 3, 'a': 3, 'b': 7},
{'t': 4, 'a': 7, 'b': 8, 'd': 2}]
如果您使用的是 Python 3.8,则该iteratools.accumulate
方法已获得一个initial
参数,因此可以将其简化为:
def updated(c, items):
c.update(items)
return c
map(dict, itertools.accumulate(x, updated, initial=collections.Counter()))
如果您只需要最终结果,而不是整个中间结果序列,则可以使用 获得functools.reduce
,当然:
import functools
>>> functools.reduce(updated, x, collections.Counter())
Counter({'t': 4, 'a': 7, 'b': 8, 'd': 2})
# dict version
>>> dict(functools.reduce(updated, x, collections.Counter()))
{'t': 4, 'a': 7, 'b': 8, 'd': 2}
推荐阅读
- php - 如何在 Laravel 中通过电子邮件发送 HTML 格式
- sql - 返回未终止 PL/SQL 过程的管理器的 Oracle 查询
- python - Scipy SLSQP 约束优化并不总是有效
- sqlite - 我怎样才能得到这个 sqlite 查询的结果?
- java - Java DAO 设计 - 返回 MAP 而不是 POJO
- python - 在 Python 中将对象标记为已删除
- javascript - 通过遍历数组来制作对象属性 - JS
- apache-spark - Spark Streams:状态使用
- angular - 在 Angular 应用程序中无法访问资产文件夹中的图像
- python - Pandas:根据另一列的值将列拆分为较小的列