Re: [BangPypers] string to list query
Below answer from Navin if good one, to make it more complex :) you can use z 'AT/CG' re.split('[A-Z]/[A-Z]',z) ['A', 'G'] re.search('[A-Z]/[A-Z]',z).group() 'T/C' using these two you can get your answer On Thu, Aug 5, 2010 at 10:15 AM, Navin Kabra navin.ka...@gmail.com wrote: On Thu, Aug 5, 2010 at 10:07 AM, Vikram K kpguy1...@gmail.com wrote: Suppose i have this string: z = 'AT/CG' How do i get this list: zlist = ['A','T/C','G'] This is a very poorly specified question. And in absence of any information about what exactly are the constraints on the input, and what is the difficulty you're trying to overcome (and indeed no information about what you tried already), I am going with the simplest solution: zlist = [z[0:1], z[1:4], z[4:5]] It looks like you're doing some DNA analysis, and I would guess that all these strings will be 5 characters, I'm sure my solution will work fine. ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers -- Nitin K ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] string to list query
On Thu, Aug 5, 2010 at 10:07 AM, Vikram K kpguy1...@gmail.com wrote: Suppose i have this string: z = 'AT/CG' How do i get this list: zlist = ['A','T/C','G'] One solution, please verify: def group_seq(seq): seq_out = [] skip = 0 seq_len = len(seq) for i,char in enumerate(seq): if skip 0: skip = skip - 1 continue if seq_len = i+1: seq_out.append(char) break if seq[i+1] == '/': seq_out.append(char+seq[i+1]+seq[i+2]) skip = 2 else: seq_out.append(char) return seq_out if __name__ == __main__: seq = AT/CG print seq, group_seq(seq) seq = A/UT/CG print seq, group_seq(seq) seq = A/UT/CG/A print seq, group_seq(seq) seq = AT/CGAAA print seq, group_seq(seq) seq = AT/CGAAG/CG/TCA print seq, group_seq(seq) Regards, Baiju M ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] string to list query
Simplified: def group_seq(seq): seq_out = [] slash_found = False for char in seq: if slash_found: seq_out[-1] = seq_out[-1]+char slash_found = False continue if char == '/': seq_out[-1] = seq_out[-1]+char slash_found = True continue seq_out.append(char) return seq_out if __name__ == __main__: seq = AT/CG print seq, group_seq(seq) seq = A/UT/CG print seq, group_seq(seq) seq = A/UT/CG/A print seq, group_seq(seq) seq = AT/CGAAA print seq, group_seq(seq) seq = AT/CGAAG/CG/TCA print seq, group_seq(seq) -- Baiju M ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] string to list query
s='AT/CG' m=re.compile(r'([A-Z]+)([A-Z]/[A-Z])([A-Z]+)', re.IGNORECASE) m.match(s).groups() ('A', 'T/C', 'G') --Anand ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] string to list query
Here's a slightly different approach def splitter(input): buffer = [] slash = False for char in input : if len(buffer) == 0 : buffer.append(char) elif char == '/' : buffer.append(char) slash = True elif slash : buffer.append(char) slash = False else : yield .join(buffer) buffer = [char] if len(buffer) 0 : yield .join(buffer) print tuple(splitter('AT/CG')) On Thu, Aug 5, 2010 at 10:07 AM, Vikram K kpguy1...@gmail.com wrote: Suppose i have this string: z = 'AT/CG' How do i get this list: zlist = ['A','T/C','G'] ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers -- blog: http://blog.dhananjaynene.com twitter: http://twitter.com/dnene ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
[BangPypers] string to list query
Suppose i have this string: z = 'AT/CG' How do i get this list: zlist = ['A','T/C','G'] ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] string to list query
On Thu, Aug 5, 2010 at 10:07 AM, Vikram K kpguy1...@gmail.com wrote: Suppose i have this string: z = 'AT/CG' How do i get this list: zlist = ['A','T/C','G'] This is a very poorly specified question. And in absence of any information about what exactly are the constraints on the input, and what is the difficulty you're trying to overcome (and indeed no information about what you tried already), I am going with the simplest solution: zlist = [z[0:1], z[1:4], z[4:5]] It looks like you're doing some DNA analysis, and I would guess that all these strings will be 5 characters, I'm sure my solution will work fine. ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers