代码之家  ›  专栏  ›  技术社区  ›  David542

按列分区,按另一列排序

  •  1
  • David542  · 技术社区  · 8 月前

    我正在使用以下值模拟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] .

    1 回复  |  直到 8 月前
        1
  •  2
  •   Andrej Kesely    8 月前

    尝试

    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),
        ],
    }