尝试
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"],
}
编辑:要获取输出中的行列表:
window_func = lambda partition_func, order_func: lambda x: {
k: list(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": [
(7, "2021/04", "Shop 1", 154552.82),
(5, "2021/05", "Shop 1", 1489774.16),
(3, "2021/06", "Shop 1", 1845662.35),
(6, "2021/06", "Shop 1", 52489.35),
],
"Shop 2": [
(1, "2021/04", "Shop 2", 341227.53),
(4, "2021/04", "Shop 2", 21487.63),
(2, "2021/05", "Shop 2", 315447.24),
(8, "2021/05", "Shop 2", 6548.49),
(9, "2021/06", "Shop 2", 387779.49),
],
}