按列分区,按列排序

问题描述 投票:0回答:1

我正在使用以下值模拟 SQL 查询:

rows = [(1, '2021/04', 'Shop 2', 341227.53), (2, '2021/05', 'Shop 2', 315447.24), (3, '2021/06', 'Shop 1', 1845662.35), (4, '2021/04', 'Shop 2', 21487.63), (5, '2021/05', 'Shop 1', 1489774.16), (6, '2021/06', 'Shop 1', 52489.35), (7, '2021/04', 'Shop 1', 154552.82), (8, '2021/05', 'Shop 2', 6548.49), (9, '2021/06', 'Shop 2', 387779.49)]

我想建立一个“窗口”函数的字典。它应该在第三列(前值:'shop1')上分区并按第二列(前值:'2021/06')排序。

所以,它应该看起来像这样:

{
    'Shop 1': ['2021/04', '2021/05', ...],
    'Shop 2': [...],
    ...
}

有没有办法做到这一点,这样我就可以定义一个带有两个参数的 lambda 函数,例如:

window_func = lambda partition_func, order_func: ...

上面的

partition_func
将是
item[2]
,order_func将是
item[3]

python python-3.x lambda functional-programming
1个回答
1
投票

尝试:

from itertools import groupby
from operator import itemgetter

rows = [
    (1, "2021/04", "Shop 2", 341227.53),
    (2, "2021/05", "Shop 2", 315447.24),
    (3, "2021/06", "Shop 1", 1845662.35),
    (4, "2021/04", "Shop 2", 21487.63),
    (5, "2021/05", "Shop 1", 1489774.16),
    (6, "2021/06", "Shop 1", 52489.35),
    (7, "2021/04", "Shop 1", 154552.82),
    (8, "2021/05", "Shop 2", 6548.49),
    (9, "2021/06", "Shop 2", 387779.49),
]

window_func = lambda partition_func, order_func: lambda x: {
    k: [order_func(v) for v in g]
    for k, g in groupby(
        sorted(x, key=lambda v: (partition_func(v), order_func(v))), partition_func
    )
}

fn = window_func(itemgetter(2), itemgetter(1))
print(fn(rows))

打印:

{
    "Shop 1": ["2021/04", "2021/05", "2021/06", "2021/06"],
    "Shop 2": ["2021/04", "2021/04", "2021/05", "2021/05", "2021/06"],
}
© www.soinside.com 2019 - 2024. All rights reserved.