[Tutor] Rép. : string to list
Luhmann
luhmann_br at yahoo.com
Thu Aug 5 15:05:56 CEST 2010
Try this:
>>> def f(mystring):
charlist = list(mystring)
tmplist = [ [''] ]
for a in charlist:
if tmplist[-1][-1] == '/' or a == '/':
tmplist[-1].append(a)
else:
tmplist.append( [a] )
return [''.join(a) for a in tmplist][1:]
>>> #Thusly:
>>> f('AT/CG')
['A', 'T/C', 'G']
>>> f('CATAT/T/CATA/C/AC/TACGAA/AGT/GGTC/GGGTCTACGATTT/A/A/GC/CCCCT/GA/C/A/T/GA/G/C/ACAAG/CC/GAGGTG/GACTCA/G/TTT/TAGT/AGAC/AT/CC/GG/CG/G/A')
['C', 'A', 'T', 'A', 'T/T/C', 'A', 'T', 'A/C/A', 'C/T', 'A', 'C', 'G', 'A', 'A/A', 'G', 'T/G', 'G', 'T', 'C/G', 'G', 'G', 'T', 'C', 'T', 'A', 'C', 'G', 'A', 'T', 'T', 'T/A/A/G', 'C/C', 'C', 'C', 'C', 'T/G', 'A/C/A/T/G', 'A/G/C/A', 'C', 'A', 'A', 'G/C', 'C/G', 'A', 'G', 'G', 'T', 'G/G', 'A', 'C', 'T', 'C', 'A/G/T', 'T', 'T/T', 'A', 'G', 'T/A', 'G', 'A', 'C/A', 'T/C', 'C/G', 'G/C', 'G/G/A']
--- En date de : Mer, 4.8.10, Vikram K <kpguy1975 at gmail.com> a écrit :
De : Vikram K <kpguy1975 at gmail.com>
Objet : [Tutor] string to list
À : tutor at python.org
Date: mercredi 4 août 2010 21 h 38
Suppose i have this string:
z = 'AT/CG'
How do i get this list:
zlist = ['A','T/C','G']
-----La pièce jointe associée suit-----
_______________________________________________
Tutor maillist - Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100805/38d96017/attachment-0001.html>
More information about the Tutor
mailing list