String Splitter Brain Teaser
Paul McGuire
ptmcg at austin.rr.com
Sun Mar 27 18:02:52 EST 2005
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 = OneOrMore( Group( marker + SLASH + marker ) | Group(
marker ) )
print genDegenList.parseString( testdata )
(prints:
[['A'], ['T'], ['T', 'G'], ['A'], ['T'], ['A', 'G']]
More information about the Python-list
mailing list