On Tue, Mar 6, 2012 at 3:07 PM, kumar s <ps_pyt...@yahoo.com> wrote: > Hi : > > I have some strings with both alpha-numeric strings. I want to add all the > numbers in that string and leave characters and special characters. > 1A0G19 > > 5G0C25^C52 > > 0G2T3T91 > 44^C70 > > How can I count only the numbers in the above. > > 1 A 0 G 19 = 1+0+19 = 20 > > 5 G 0 C 25 ^C 52 = 5+0+25+52 = 82 > > 0 G 2 T 3 T 91 = 0+2+3+91 = 96 > 44 ^C 70 = 44+70 = 114 > > In first string 1A0G19 I am only adding 1, 0, and 19. I am not splitting > 19 to add 1+9 which will give totally wrong answer for me. > > > Is there a way I can do this. > > Thanks for your advise. > > kumar > > _______________________________________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor
I am pretty much a novice with regular expressions, but this seems to work to find all the numeric sequences: >>> source = 'ab123t&4533www' >>> re.findall('\d*', source) ['', '', '123', '', '', '4533', '', '', '', ''] >>> so then something like this: total = 0 my_list = re.findall('\d*', source) for my_number in my_list: if my_number != '' total += int(my_number) -- Joel Goldstick _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor