I am using Python 3.8 and I am attempting to write a lambda expression to square even numbers and cube odd numbers. So far, the code I have come for is as follows:
l = list(range(1, 11))
list(filter(lambda x: x ** 2 if x % 2 == 0 else x ** 3, l))
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The syntax for if, else using lambda is:
lambda <arguments> : <Return Value if condition is True> if <condition> else <Return Value if condition is False>
However, the lambda expression is not doing the job. Help?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…