2

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()
2
  • 4
    You're filtering in different orders. Commented Jan 18, 2018 at 13:16
  • The filters for e are applied inside-out. Meaning, the lambda x : x > 10 is applied first. Essentially, the reverse order is followed for e. Commented Jan 18, 2018 at 13:23

1 Answer 1

3

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)))))
Sign up to request clarification or add additional context in comments.

1 Comment

Stylish answer. Using list comprehension it would look like e = [ 2*y for y in [ 3*x for x in numbers if x > 3 ] if y > 10 ].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.