>>> def accept(d1, d2):
    if somefunc(d1,d2) > 32:
        h = 1
    else:
        h = 0
    return h

Does Python have a ternary conditional operator? doesn't give a solution for a case one want to return a value. A lambda based solution is preferable.

有帮助吗?

解决方案 2

Or, perhaps trickier,

return int(somefunc(d1, d2) > 32)

Note that int(True) == 1 and int(False) == 0.

其他提示

The "return-value scenario" is no different than any other:

return 1 if somefunc(d1, d2) > 32 else 0

If for some reason you want a lambda:

lambda d1, d2: 1 if somefunc(d1, d2) > 32 else 0

Note that a lambda is no different than a function defined with def that returns the same thing. Lambdas are just regular functions.

Turn into a lambda (not a explicit function):

accept = lambda d1,d2: 1 if somefunc(d1, d2) > 32 else 0
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top