Is it possible to have multiple statements in a python lambda expression?

后端 未结 18 1935
感情败类
感情败类 2020-12-12 21:33

I am a python newbie trying to achieve the following:

I have a list of lists:

lst = [[567,345,234],[253,465,756, 2345],[333,777,111, 555]]

18条回答
  •  执笔经年
    2020-12-12 21:55

    You can in fact have multiple statements in a lambda expression in python. It is not entirely trivial but in your example, the following works:

    map(lambda x: x.sort() or x[1],lst)
    

    You have to make sure that each statement does not return anything or if it does wrap it in (.. and False). The result is what is returned by the last evaluation.

    Example:

    >>> f = (lambda : (print(1) and False) or (print(2) and False) or (print(3) and False))
    >>> f()
    1
    2
    3
    

提交回复
热议问题