Brian Scearce <b...@pathetique.com> added the comment:

This was closed over a year ago, but since mark.dickinson was asking for 
convincing use-cases: I'm breaking up a file into line-delimited chunks.  These 
chunks are non-overlapping, contiguous, and tend to be fairly large, so I'm 
just recording the start line of each chunk in a 2-ple:

mapping = [
  (10, 'first chunk'),
  (50, 'second chunk'),
  (60, 'third chunk')
]

Lines 10-49 are in the first chunk, lines 50-59 are in the second, lines 60+ 
are in the third.  So:

def CategorizeLine(line, mapping):
   loc = bisect.bisect([m[0] for m in mapping], line)
   if loc == 0:
      return None # before first chunk
   return mapping[loc-1][1]

It Would Be Nice if I could write the second line as:

   loc = bisect.bisect(mapping, line, key=lambda m:m[0])

The bisect documentation suggests pre-computing the key list, but it seems 
messy and error-prone to keep a redundant data structure in sync with its 
source.  I could also rewrite my "mapping" data structure to be two parallel 
lists instead of one list of 2-ples, but this data structure is more readable 
and extensible and handles empty lists more naturally.

----------
nosy: +bls

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue4356>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to