| Store | Cart

String Splitter Brain Teaser

From: Brian van den Broek <bva...@po-box.mcgill.ca>
Sun, 27 Mar 2005 20:24:07 -0500
Michael Spencer said unto the world upon 2005-03-27 20:04:
> James Stroud wrote:> >> Hello,>>>> I have strings represented as a combination of an alphabet (AGCT) and >> a an operator "/", that signifies degeneracy. I want to split these >> strings into lists of lists, where the degeneracies are members of the >> same list and non-degenerates are members of single item lists. An >> example will clarify this:>>>> "ATT/GATA/G">>>> gets split to>>>> [['A'], ['T'], ['T', 'G'], ['A'], ['T'], ['A', 'G']]>>> >  >>> def group(src):>  ...     stack = []>  ...     srciter = iter(src)>  ...     for i in srciter:>  ...         if i == "/":>  ...             stack[-1].append(srciter.next())>  ...         else:>  ...             stack.append([i])>  ...     return stack>  ...>  >>> group("ATT/GATA/G")>  [['A'], ['T'], ['T', 'G'], ['A'], ['T'], ['A', 'G']]>  >>>> > Michael> 

Much nicer than mine. =| :-)

                         ^
                         |
                         ---- (hats off)

I've got to get iterators into my working vocabulary!

Best,

Brian vdB

Recent Messages in this Thread
James Stroud Mar 27, 2005 10:39 pm
Brian van den Broek Mar 28, 2005 12:09 am
Michael Spencer Mar 28, 2005 01:04 am
Brian van den Broek Mar 28, 2005 01:24 am
Michael Spencer Mar 28, 2005 02:06 am
James Stroud Mar 28, 2005 01:46 am
Messages in this thread