+ 1

Python optimisation

how can I optimize if a<0 and b<0 and c<0 : # code ??

25th Oct 2018, 10:08 PM
NoxFly
NoxFly - avatar
6 Answers
+ 10
if all([a, b, c]) < 0: # code pass
25th Oct 2018, 10:24 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
25th Oct 2018, 10:24 PM
NoxFly
NoxFly - avatar
+ 2
have you an operation to reduce the "a<0 and b<0 and c<0" ?
25th Oct 2018, 10:15 PM
NoxFly
NoxFly - avatar
+ 1
what do you want to optimize or how ?
25th Oct 2018, 10:14 PM
Mbrustler
Mbrustler - avatar
+ 1
wait a moment i will look edited: not that efficient but i,*d = 0,a,b,c if all(d)<0: print(yes)
25th Oct 2018, 10:18 PM
Mbrustler
Mbrustler - avatar
+ 1
Except all() always returns a boolean, so the check `all(iterable) < 0` will never be true. Besides, what's there to be "optimized"? The code's already readable and concise at least for a check of just three parameters long. Anyway, the correct approach with all() for your sample test would be `all(x < 0 for x in (a, b, c))`.
25th Oct 2018, 11:34 PM
Edgar Garrido
Edgar Garrido - avatar