Lambda函数转换

时间:2016-08-23 10:23:37

标签: python lambda

我一直在努力处理一段我无法理解的python代码。 它是一个嵌套的lambda函数,如下所示:

lambda l: lambda x: x[0] in [None if not i.object else i.object.key for i in l]

我试图翻译它,我想出了这个,但我不认为这是对的

 def f1(l):
     def f2(x):
           for i in l:
               if not i.object:
                  return None
               else
                  return x[0]

1 个答案:

答案 0 :(得分:2)

第1步:

def f1(l):
    def f2(x):
        xs = [None if not i.object else i.object.key for i in l]
        return x[0] in xs
    return f2

第2步:

def f1(l):
    def f2(x):
        xs = []
        for i in l:
            if not i.object:
                xs.append(None)
            else:
                xs.append(i.object.key)
        return x[0] in xs
    return f2