Python,使用lambda,map和filter

时间:2018-01-18 13:15:10

标签: python list lambda filter mapping

E是a,b,c和d的组合。 D是最终结果。 所以e的结果应该与d相同。但事实并非如此。我做错了什么?

结果d = [24,42,30,42,48,36]

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()

1 个答案:

答案 0 :(得分:2)

问题在于,当您计算e时,您会按照计算d时的相反顺序执行操作。尝试像这样计算e

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