【问题标题】:Python groupby by two or more columnsPython groupby 按两列或多列
【发布时间】:2015-05-20 16:39:33
【问题描述】:

有人能解释一下为什么我不能得到第二个索引的总和(传入)。我试图在第一次迭代之后打印变量 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}

【问题讨论】:

    标签: python loops dictionary group-by


    【解决方案1】:

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

    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]: 
    

    【讨论】:

      猜你喜欢
      • 2021-02-08
      • 2021-05-30
      • 2013-06-13
      相关资源
      最近更新 更多