Today, my colleagues use Lambda in ironpython to writeProgram(Our program uses the lambda function of ironpython) and finds a problem. Suppose there is a function: c = A/B, but B may be 0. If it is 0, so we want c = 0. Because it is a Lambda expression, we must use a line of description. Unfortunately, I found that ironpython does not support the ternary operator. Later I checked the information, I found that snowdream wrote a solution: Python Study Notes (3 ).
The modified program is:B! = 0 and A/B or 0Note that the difference is not equal here. We find that when B is 0, it does not calculate a/B, which serves our purpose.
Supplement
Today, I encountered this kind of problem again. I searched the network again and found that there was actually a smooth writing:
Result = A/B if B <> 0 else 0
That is, if B is not equal to 0, the calculation expression. Otherwise, 0 is returned.
Supplement:
I am so stupid. Someone has pointed out this method for replying last year. I have not paid attention to it yet. Whining, hitting your ass.