Python 按两列或多列分组

Python groupby by two or more columns

有人可以解释一下为什么我无法获得第二个索引(传入)的总和。我试图在第一次迭代之后打印变量 group 的值,似乎一旦它被迭代就没有更多的值。

我从这个例子中提取了一些代码。 example post

products = {}
products['product']= [ {
                    'id':1234,
                    'stock_id':1001,
                    'lot_id':5001,
                    'name':'product1',
                    'qty':50,
                    'incoming':100,
                }
                .................
                .................
           ]

grouper = itemgetter("id","stock_id","lot_id")
result = []

for key, group in groupby(sorted(products['product'], key= grouper),grouper):    
temp_dict= dict(zip(["id","stock_id","lot_id"], key))
temp_dict["qty"] = sum(item["qty"] for item in group)
temp_dict["incoming"]  = sum(item["incoming"] for item in group)

result.append(temp_dict)



for r in result:
  print r

结果

{'lot_id': 5001, 'stock_id': 1001, 'incoming': 0, 'id': 1234, 'qty': 250}
{'lot_id': 5001, 'stock_id': 1001, 'incoming': 0, 'id': 1235, 'qty': 50}
{'lot_id': 5002, 'stock_id': 1001, 'incoming': 0, 'id': 1235, 'qty': 100}
{'lot_id': 5001, 'stock_id': 1002, 'incoming': 0, 'id': 1236, 'qty': 100}

您在第一个总和中使用组迭代器,在组group = list(group)上调用列表将内容存储在列表中,以便您可以使用它们两次:

for key, group in groupby(sorted(products['product'], key=grouper), grouper):
    temp_dict = dict(zip(["id", "stock_id", "lot_id"], key))
    group = list(group)
    temp_dict["qty"] = sum(item["qty"] for item in group)
    temp_dict["incoming"] = sum(item["incoming"] for item in group)

你基本上是在做:

In [4]: group = iter([1,2,3,4])

In [5]: for ele in group: # iterator will be consumed
           print(ele)
   ...:     
1
2
3
4

In [6]: for ele in group: # nothing left to iterate
           print(ele)
   ...:     

In [7]: