这就是我想要的:
cr.execute(#some query)
lines = self.cr.dictfetchall()
total_qty = 0.00
total_weight = 0.00
total_weight_net = 0.00
total_volume = 0.00
for line in lines:
total_qty += line['product_qty']
total_weight += line['weight']
total_volume += line['volume']
有没有更好的方法来做这种总结?谢谢
答案 0 :(得分:0)
您可能正在尝试寻找替代循环方法:
lines = [{"a": 10, "b":22},
{"a": 11, "b":22},
{"a": 12, "b":20},
{"a": 15, "b":15},
{"a": 15, "b":12}]
sumA = sum(itm["a"] for itm in lines)
sumB = sum(itm["b"] for itm in lines)
print sumA
print sumB
打印
63
91
无论如何,正如@MartijnPieters建议的那样,通过sql查询获得总和很可能会更快。