dgadiraju
12/17/2017 - 8:41 AM

Get revenue for each order

Get revenue for each order

import itertools as it
import sys
def getRevenuePerOrder(orderItems):
#2,2,1073,1,199.99,199.99
#3,2,502,5,250.0,50.0
#4,2,403,1,129.99,129.99
  orderItemsMap = it.imap(lambda oi: (int(oi.split(",")[1]), float(oi.split(",")[4])), orderItems)
#(2,199.99)
#(2,250.0)
#(2,129.99)
  orderItemsGroupBy = it.groupby(sorted(orderItemsMap), lambda k: k[0])
#(2, [(2,199.99), (2,250.0), (2,129.99)])
  revenuePerOrder = it.imap(lambda r: (r[0], sum(map(lambda v: v[1], r[1]))), orderItemsGroupBy)  
  return revenuePerOrder

orderItems = open(sys.argv[1]).read().splitlines()
for i in list(getRevenuePerOrder(orderItems))[:10]: print(i)