Map returns a blank list


#1

The code below returns the following error. Can anyone suggest why is the map function returning a blank collection?

> import itertools as it
>     def revenue_per_orderID(oid):
>         order_items = open('D:\itversity\data\data\\retail_db\\order_items\\part-00000').read().splitlines()
>         req_records=it.ifilter(lambda rr:int(rr.split(',')[1])==oid,order_items)
>         print list(req_records)
>         req_map=map(lambda f:float(f.split(',')[4]),list(req_records))
>         print (req_map)
>         r=reduce(lambda x,y:x+y,req_map)
>         return r
> revenue_per_orderID(2)

Error:

C:\Python27\python.exe C:/Users/IND/PycharmProjects/itversity_demo/revenuePerOrderId.py

Traceback (most recent call last):
File “C:/Users/IND/PycharmProjects/itversity_demo/revenuePerOrderId.py”, line 11, in
[‘2,2,1073,1,199.99,199.99’, ‘3,2,502,5,250.0,50.0’, ‘4,2,403,1,129.99,129.99’]
revenue_per_orderID(2)
[]
File “C:/Users/IND/PycharmProjects/itversity_demo/revenuePerOrderId.py”, line 8, in revenue_per_orderID
r=reduce(lambda x,y:x+y,req_map)
TypeError: reduce() of empty sequence with no initial value

Process finished with exit code 1


#2

@purbita_biswas It would work if you convert the req_records to a list before doing a map. Here is a code which works

import sys
import itertools as it


def revenue_per_order_id(oid):
    inputPath = sys.argv[2]
    order_items = open(inputPath + 'order_items/part-00000').\
        read().splitlines()

    req_records = it.ifilter(lambda rr: int(rr.split(',')[1]) == oid, order_items)
    req_records_list = list(req_records)

    print req_records_list

    req_map = map(lambda f: float(f.split(",")[4]), req_records_list)

    print (req_map)

    r = reduce(lambda x, y: x+y, req_map)
    return r


print revenue_per_order_id(2)

The function returns 579.98 as its output


#3

What is the importance of importing ‘sys’ module here? for the sys.argv line I get the following error:

C:\Python27\python.exe C:/Users/IND/PycharmProjects/itversity_demo/sys_argv_test.py
Traceback (most recent call last):
File “C:/Users/IND/PycharmProjects/itversity_demo/sys_argv_test.py”, line 22, in
print revenue_per_order_id(2)
File “C:/Users/IND/PycharmProjects/itversity_demo/sys_argv_test.py”, line 6, in revenue_per_order_id
inputPath = sys.argv[2]
IndexError: list index out of range

Second question:

I got the code working by taking a few differences from your code:

Your working code snippet:

req_records_list=list(req_records)
req_map = map(lambda f: float(f.split(",")[4]), req_records_list)

my code snippet that returned blank list:

req_map=map(lambda f:float(f.split(',')[4]),list(req_records))

The difference is, I tried the typecast to list within the map method itself. Apparently, that is wrong for python but, is there any explanation how?


#4

sys module provides access to system specific parameters, Please check the standard library, You are getting index out of bound so probably you are not separating the parameters by space in Python run conf.

Coming to your second question, I am not sure about internals of Python but probably its because for every map step it is converting the records to a list


#5

Just copy pasted your code to PyCharm. It’s not asking for any parameters


#6

I have added the parameters in the run configuration of Pycharm