On Wed, Jun 29, 2011 at 3:32 AM, Ellerbee, Edward <eeller...@bbandt.com> wrote: > How to condense a group of numbers to a wildcard list. For example: > > 252205 > 252206 > 252208 > > Condense to: > 25220[568]
Assuming you have the list sorted (if not, that's the first place to start), I'd do a six-pass approach - one pass for each digit. It's simple and readable, and I doubt you'll be working with enough numbers for its inefficiency to be a problem. Something like this (untested code): lastprefix=tails=None for cur in list_of_numbers: prefix=cur[:5]; tail=cur[5] if prefix!=lastprefix: if lastprefix!=None: if len(tails)>1: emit("%s[%s]"%(lastprefix,tails)) else emit(lastprefix+tails) lastprefix,tails=prefix,"" tails+=tail if lastprefix!=None: if len(tails)>1: emit("%s[%s]"%(lastprefix,tails)) else emit(lastprefix+tails) Feed the emitted results from this pass into the next pass, in which prefix is cur[:4] and tail is cur[4], etc. For the subsequent passes, you'll also need to worry about a suffix (which would be cur[5:] when you're looking at cur[4]), and ensure that that matches, same as the prefix. This is completely untested, but it should be a start. (Replace the calls to the imaginary "emit" function with whatever you do to emit results - for the intermediate passes, that's probably appending to a list.) Chris Angelico -- http://mail.python.org/mailman/listinfo/python-list