plz help

0 views
Skip to first unread message

sandy

unread,
Apr 25, 2009, 6:37:27 AM4/25/09
to PyHou - Python In Houston
hi can u plz help me to get substrings between two characters at each
occurence
for eg:
to get all the substrings between Q and E in the given example
sequence in all occurences.
ex: QUWESEADFQDFSAEDFS

and to find the substring with minimum length.

plz help me

Walker Hale IV

unread,
Apr 25, 2009, 6:25:34 PM4/25/09
to py...@googlegroups.com
I assume you want to exclude overlapping substrings. Otherwise you
will get a combinatorial explosion of related results.

Code follows. -Walker

import re

PAT = re.compile(r'Q([^QE]+)E')

def findAllHits(s):
"""Find all non-overlapping substrings between Q and E in s."""
return PAT.findall(s)

def findShortest(l):
"""Find the list of shortest strings in l. Return l if l is empty."""
if not l:
return l
i = iter(l)
shortest = [i.next()] # Grap the first as the possible shortest string.
length = len(shortest[0])
for s in i: # Loop over the remaining strings (if any).
if len(s) < length: # New shortest string.
length = len(s)
shortest = [s]
elif len(s) == length: # A tie for shortest string.
shortest.append(s)
return shortest

if __name__ == '__main__':
# Demo.
hits = findAllHits('QUWESEADFQDFSAEDFS')
short = findShortest(hits)
print hits
print short

--
Walker Hale <walker....@gmail.com>

Reply all
Reply to author
Forward
0 new messages