On 4/6/2010 9:20 PM Steven D'Aprano said...
On Tue, 06 Apr 2010 16:54:18 +0000, Duncan Booth wrote:
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)]


Isn't that an awfully heavyweight and obfuscated solution for choosing
between five options? Fifty-five options, absolutely, but five?


Would it be easier to digest as:

from bisect import bisect as selectindex #

WEIGHTLIMITS = [100, 250, 500, 1000]
POSTAGEAMOUNTS = [44, 60, 80, 100, 120]

postage = POSTAGEAMOUNTS[selectindex(WEIGHTLIMITS, weight)]

---

I've used bisect this way for some time -- I think Tim may have pointed it out -- and it's been handy ever since.

Emile


--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to