[Tutor] string to list

Sander Sweers sander.sweers at gmail.com
Thu Aug 5 09:40:55 CEST 2010


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


More information about the Tutor mailing list