[Tutor] string to list

Alan Gauld alan.gauld at btinternet.com
Thu Aug 5 12:44:47 CEST 2010


"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']
>
There are lots of ways and it really needs a tighter specification of 
yhow you split.

What would "AT/C/DGH" give for example?

But in general there are several approaches, I suspect regex is the 
best
solution here but for simple solutions splitting by the / then 
splitting the
sublists then joining the last to the first element of each sublist 
using / might
work too. But regex is probably faster, although more complex.

And you could use a parser if the rules are very complex.

HTH,
-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/





More information about the Tutor mailing list