Python convtools 展示:流行聚合

今天,我想演示一下 convtools 如何帮助你避免重复实施流行的聚合。

将对象序列转换为字典,其中的值按和计算

from convtools import conversion as c

func = c.aggregate(
    c.ReduceFuncs.DictSum(c.item("a"), c.item("b"))
).gen_converter()

data = [
    {"a": 1, "b": 10},
    {"a": 2, "b": 11},
    {"a": 1, "b": None},
    {"a": 2, "b": 22},
    {"a": 1, "b": 23},
    {"a": 2, "b": 24},
]
assert func(data) == {1: 33, 2: 57}

将数值汇总到列表中

此外,在某些情况下,定义条件和/或要求值是唯一的也很有用。

from convtools import conversion as c

func = (
    c.group_by(c.item("a"))
    .aggregate(
        {
            "a": c.item("a"),
            "v1": c.ReduceFuncs.Array(
                c.item("b"),
                where=c.item("b").is_not(None),
            ),
            "v2": c.ReduceFuncs.ArrayDistinct(c.item("b")),
        }
    )
    .gen_converter()
)

data = [
    {"a": 1, "b": 10},
    {"a": 2, "b": 11},
    {"a": 1, "b": None},
    {"a": 2, "b": 22},
    {"a": 1, "b": 23},
    {"a": 2, "b": 22},
]
assert func(data) == [
    {"a": 1, "v1": [10, 23], "v2": [10, None, 23]},
    {"a": 2, "v1": [11, 22, 22], "v2": [11, 22]},
]

按键分组并找出最大值的一行

from convtools import conversion as c

func = (
    c.group_by(c.item("name"))
    .aggregate(
        {
            "name": c.item("name"),
            "top_product": c.ReduceFuncs.MaxRow(c.item("sales")).item(
                "product"
            ),
        }
    )
    .gen_converter()
)
data = [
    {"name": "a", "product": "p1", "sales": 11},
    {"name": "a", "product": "p2", "sales": 10},
    {"name": "b", "product": "p1", "sales": 21},
    {"name": "a", "product": "p4", "sales": 13},
    {"name": "a", "product": "p3", "sales": 12},
    {"name": "b", "product": "p2", "sales": 22},
]
assert func(data) == [
    {"name": "a", "top_product": "p4"},
    {"name": "b", "top_product": "p2"},
]

如需了解 convtools 还包含哪些减速器,请点击以下链接: https://convtools.readthedocs.io/en/latest/aggregations/#creducefuncs

如果您希望我介绍什么特别的话题,请在评论中告诉我。
注意安全,下期节目再见。

你可能感兴趣的:(python,后端,低代码)