我正在使用以下值模拟 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]
。
尝试:
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"],
}