(a==b) ? 'Yes' : 'No'

Duncan Booth duncan.booth at invalid.invalid
Tue Apr 6 12:54:18 EDT 2010


Albert van der Horst <albert at spenarnc.xs4all.nl> wrote:

> Old hands would have ...
>     stamp =( weight>=1000 and  120 or
>              weight>=500  and  100 or
>              weight>=250  and  80  or
>              weight>=100  and  60  or
>                                44  )
> 
> (Kind of a brain twister, I think, inferior to C, once the c-construct
> is accepted as idiomatic.)

I doubt many old hands would try to join multiple and/or operators that 
way. Most old hands would (IMHO) write the if statements out in full, 
though some might remember that Python comes 'batteries included':

 from bisect import bisect
 WEIGHTS = [100, 250, 500, 1000]
 STAMPS = [44, 60, 80, 100, 120]

 ...
 stamp = STAMPS[bisect(WEIGHTS,weight)]

>>> map(lambda weight: STAMPS[bisect(WEIGHTS, weight)], [20, 100, 150, 999, 
1000, 1100])
[44, 60, 60, 100, 120, 120]



More information about the Python-list mailing list