bhr
bhr

Reputation: 555

Python, using lambda, map and filter

E is the combination of a, b, c and d. D is the final result. So the result of e should be the same of d. But it's not. What am I doing wrong?

result of d = [24, 42, 30, 42, 48, 36]

result of e = [42, 42, 48, 36]

numbers = [2,4,7,2,5,3,7,8,1,6]

def mapping():
    a = list(filter(lambda x : x > 3, numbers))
    print(a)
    b = list(map(lambda x : x * 3, a))
    print(b)
    c = list(filter(lambda x : x > 10, b))
    print(c)
    d = list(map(lambda x : x * 2, c))
    print(d)

    e = list(filter(lambda x : x > 3, map(lambda x : x * 3, filter(lambda x : x > 10, map(lambda x : x * 2, numbers)))))
    print(e)
mapping()

Upvotes: 2

Views: 1942

Answers (1)

The problem is that when you you compute e, you do the operations in the inverse order of when you compute d. Try to compute e like this:

e = list(map(lambda x : x * 2, 
             filter(lambda x : x > 10, 
                    map(lambda x : x * 3, 
                        filter(lambda x : x > 3, numbers)))))

Upvotes: 3

Related Questions