Re: String Splitter Brain Teaser

2005-03-28 Thread Doug Schwarz
In article <[EMAIL PROTECTED]>, James Stroud <[EMAIL PROTECTED]> 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

Re: String Splitter Brain Teaser

2005-03-28 Thread Doug Schwarz
In article <[EMAIL PROTECTED]>, James Stroud <[EMAIL PROTECTED]> 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

Re: String Splitter Brain Teaser

2005-03-28 Thread Bill Mill
> That is clearer. At this point, though, you don't need the enumerator any > more > (so you can avoid indexing each item): Good point. > > def xgen(s): > srciter = iter(s) > item = [srciter.next()] > for i in srciter: > if i == '/': > item.append(srciter.

Re: String Splitter Brain Teaser

2005-03-28 Thread Steven Bethard
Michael Spencer wrote: def xgen(s): srciter = iter(s) item = [srciter.next()] for i in srciter: if i == '/': item.append(srciter.next()) else: yield item item = [i] yield item Note that the generator-based solution doesn't generate

Re: String Splitter Brain Teaser

2005-03-28 Thread Michael Spencer
Bill Mill wrote: > [long genomes might justify a generator approach] That's a good point. I should have said: *If* you are going to put the items into a list anyway, then there is no point generating the list items individually. Michael Spencer wrote: >>[Bill's solution didn't work for multiple-

Re: String Splitter Brain Teaser

2005-03-28 Thread Bill Mill
On Mon, 28 Mar 2005 09:18:38 -0800, Michael Spencer <[EMAIL PROTECTED]> wrote: > Bill Mill wrote: > > > for very long genomes he might want a generator: > > > > def xgen(s): > > l = len(s) - 1 > > e = enumerate(s) > > for i,c in e: > > if i < l and s[i+1] == '/': > >

Re: String Splitter Brain Teaser

2005-03-28 Thread Michael Spencer
Bill Mill wrote: for very long genomes he might want a generator: def xgen(s): l = len(s) - 1 e = enumerate(s) for i,c in e: if i < l and s[i+1] == '/': e.next() i2, c2 = e.next() yield [c, c2] else: yield [c] for g in xge

Re: String Splitter Brain Teaser

2005-03-28 Thread Bill Mill
On 28 Mar 2005 04:12:15 -0800, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > This is shorter: > map(list,' '.join(s).replace(' / ','').split()) > > but for very long genomes Michael Spencer's nice version can be faster. > for very long genomes he might want a generator: def xgen(s): l = le

Re: String Splitter Brain Teaser

2005-03-28 Thread bearophileHUGS
This is shorter: map(list,' '.join(s).replace(' / ','').split()) but for very long genomes Michael Spencer's nice version can be faster. Hugs, Bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: String Splitter Brain Teaser

2005-03-28 Thread runsun pan
For the fans of funtional programming: >>> s='ATT/GATA/G' >>> [y.split('/') for y in (' '.join([x for x in s]).replace(' / ', '/')).split()] [['A'], ['T'], ['T', 'G'], ['A'], ['T'], ['A', 'G']] -- http://mail.python.org/mailman/listinfo/python-list

Re: String Splitter Brain Teaser

2005-03-27 Thread Mike Rovner
Jp Calderone wrote: On Sun, 27 Mar 2005 14:39:06 -0800, James Stroud <[EMAIL PROTECTED]> wrote: "ATT/GATA/G" gets split to [['A'], ['T'], ['T', 'G'], ['A'], ['T'], ['A', 'G']] I have written a very ugly function to do this (listed below for the curious), but intuitively I think this should only ta

Re: String Splitter Brain Teaser

2005-03-27 Thread Ron_Adam
On Sun, 27 Mar 2005 14:39:06 -0800, James Stroud <[EMAIL PROTECTED]> 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 th

Re: String Splitter Brain Teaser

2005-03-27 Thread Michael Spencer
Brian van den Broek wrote: Much nicer than mine. =| :-) ^ | (hats off) Cool ascii art (but thanks for the translation)! Michael -- http://mail.python.org/mailman/listinfo/python-list

Re: String Splitter Brain Teaser

2005-03-27 Thread James Stroud
On Sunday 27 March 2005 05:04 pm, Michael Spencer wrote: >   >>> def group(src): >   ...     stack = [] >   ...     srciter = iter(src) >   ...     for i in srciter: >   ...         if i == "/": >   ...             stack[-1].append(srciter.next()) >   ...         else: >   ...             stack.app

Re: String Splitter Brain Teaser

2005-03-27 Thread Brian van den Broek
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 s

Re: String Splitter Brain Teaser

2005-03-27 Thread Michael Spencer
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 lis

Re: String Splitter Brain Teaser

2005-03-27 Thread Brian van den Broek
James Stroud said unto the world upon 2005-03-27 17:39: 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-degene

Re: String Splitter Brain Teaser

2005-03-27 Thread Doug Schwarz
In article <[EMAIL PROTECTED]>, James Stroud <[EMAIL PROTECTED]> 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

Re: String Splitter Brain Teaser

2005-03-27 Thread Jp Calderone
On Sun, 27 Mar 2005 14:39:06 -0800, James Stroud <[EMAIL PROTECTED]> 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

Re: String Splitter Brain Teaser

2005-03-27 Thread Paul McGuire
Using a parser may sound like overkill, but why not when it's this easy? Get the latest pyparsing at http://pyparsing.sourceforge.net. -- Paul from pyparsing import oneOf, Group, OneOrMore, Literal testdata = "ATT/GATA/G" marker = oneOf( "A T G C") SLASH = Literal("/").suppress() genDegenList

String Splitter Brain Teaser

2005-03-27 Thread James Stroud
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