[Tutor] string to list

Thomas C. Hicks paradox at pobox.com
Thu Aug 5 12:13:55 CEST 2010


On Thu, 5 Aug 2010 03:40:55 -0400
Sander Sweers <sander.sweers at gmail.com> wrote:

> On 5 August 2010 06:38, Vikram K <kpguy1975 at gmail.com> wrote:
> > Suppose i have this string:
> > z = 'AT/CG'
> >
> > How do i get this list:
> >
> > zlist = ['A','T/C','G']
> 
> If you know the format of the string is always the same you can do
> something like this. This fails when you have strings that do not have
> the '/' in the middle and has 2 characters on either side.
> 
> def parseString(s):
> 	n = s.find('/')
> 	l = []
> 	if n:
> 		l = [s[0],s[n-1:n+2], s[-1]]
> 	return l
> 
> >>> parseString(s)
> ['A', 'T/C', 'G']
> 
> Greets
> Sander

Much better than my original thought on this!  If the data is
stereotypic couldn't you simplify it though? ...

z='AT/CG'
zlist=[z[0],z[1:4],z[-1]]

tom



More information about the Tutor mailing list