The syntax you're looking for:
lambda x: True if x % 2 == 0 else False
But you can't use print or raise in a lambda.
The syntax you're looking for:
lambda x: True if x % 2 == 0 else False
But you can't use print or raise in a lambda.
why don't you just define a function?
def f(x):
if x == 2:
print(x)
else:
raise ValueError
there really is no justification to use lambda in this case.
Python lambda with if but without else - Stack Overflow
can I use an if statement with a lambda function?
Using the if, else statement with reduce() and lambda()
Lambda if statement
Videos
Use the exp1 if cond else exp2 syntax.
rate = lambda T: 200*exp(-T) if T>200 else 400*exp(-T)
Note you don't use return in lambda expressions.
The right way to do this is simple:
def rate(T):
if (T > 200):
return 200*exp(-T)
else:
return 400*exp(-T)
There is absolutely no advantage to using lambda here. The only thing lambda is good for is allowing you to create anonymous functions and use them in an expression (as opposed to a statement). If you immediately assign the lambda to a variable, it's no longer anonymous, and it's used in a statement, so you're just making your code less readable for no reason.
The rate function defined this way can be stored in an array, passed around, called, etc. in exactly the same way a lambda function could. It'll be exactly the same (except a bit easier to debug, introspect, etc.).
From a comment:
Well the function needed to fit in one line, which i didn't think you could do with a named function?
I can't imagine any good reason why the function would ever need to fit in one line. But sure, you can do that with a named function. Try this in your interpreter:
>>> def foo(x): return x + 1
Also these functions are stored as strings which are then evaluated using "eval" which i wasn't sure how to do with regular functions.
Again, while it's hard to be 100% sure without any clue as to why why you're doing this, I'm at least 99% sure that you have no reason or a bad reason for this. Almost any time you think you want to pass Python functions around as strings and call eval so you can use them, you actually just want to pass Python functions around as functions and use them as functions.
But on the off chance that this really is what you need here: Just use exec instead of eval.
You didn't mention which version of Python you're using. In 3.x, the exec function has the exact same signature as the eval function:
exec(my_function_string, my_globals, my_locals)
In 2.7, exec is a statement, not a function—but you can still write it in the same syntax as in 3.x (as long as you don't try to assign the return value to anything) and it works.
In earlier 2.x (before 2.6, I think?) you have to do it like this instead:
exec my_function_string in my_globals, my_locals
A lambda, like any function, must have a return value.
lambda x: x if (x<3) does not work because it does not specify what to return if not x<3. By default functions return None, so you could do
lambda x: x if (x<3) else None
But perhaps what you are looking for is a list comprehension with an if condition. For example:
In [21]: data = [1, 2, 5, 10, -1]
In [22]: [x for x in data if x < 3]
Out[22]: [1, 2, -1]
I found that filter provided exactly what I was looking for in python 2:
>>> data = [1, 2, 5, 10, -1]
>>> filter(lambda x: x < 3, data)
[1, 2, -1]
The implementation is different in 2.x and 3.x: while 2.x provides a list, 3.x provides an iterator. Using a list comprehension might make for a cleaner use in 3.x:
>>> data = [1, 2, 5, 10, -1]
>>> [filter(lambda x: x < 3, data)]
[1, 2, -1]
writing an if statement with a lambda function:
I'm trying to filter a map in spark using an if statement but get a syntax error. I have not been able to find the error. Can you guys tell me what I am doing wrong here?
full_count_with0val = full_rdd.map(lambda x: json.loads(x[1])).flatMap(lambda x: x['exposures']).map(lambda x: x['pdd_list'] if len(x['pdd_list'])==0).take(5)