[Tutor] count numbers only in the string

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[Tutor] count numbers only in the string

kumar s
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  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] count numbers only in the string

Prasad, Ramit-2
> -----Original Message-----
> From: tutor-bounces+ramit.prasad=[hidden email] [mailto:tutor-
> bounces+ramit.prasad=[hidden email]] On Behalf Of kumar s
> Sent: Tuesday, March 06, 2012 2:08 PM
> To: [hidden email]
> Subject: [Tutor] count numbers only in the string
>
> 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
>

There might be a better way, but you can use regular expressions.

>>> match = re.search( '(\d+)A(\d+)G(\d+)', '1A0G19' )
>>> map(int, match.groups() )
[1, 0, 19]
>>> sum( _ )
20


Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] count numbers only in the string

Joel Goldstick-2
In reply to this post by kumar s
On Tue, Mar 6, 2012 at 3:07 PM, kumar s <[hidden email]> 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  -  [hidden email]
> 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  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] count numbers only in the string

Joel Goldstick-2
On Tue, Mar 6, 2012 at 3:49 PM, Joel Goldstick <[hidden email]> wrote:

> On Tue, Mar 6, 2012 at 3:07 PM, kumar s <[hidden email]> 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  -  [hidden email]
>> 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

Here is a shorter way:

>>> source = 'ab123t&4533www'
>>> my_list = re.findall('\d*', source)
>>> my_list
['', '', '123', '', '', '4533', '', '', '', '']
>>> sum(int(s) for s in my_list if s != '')
4656


--
Joel Goldstick
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] count numbers only in the string

Joel Goldstick-2
On Tue, Mar 6, 2012 at 3:58 PM, Joel Goldstick <[hidden email]> wrote:

> On Tue, Mar 6, 2012 at 3:49 PM, Joel Goldstick <[hidden email]> wrote:
>> On Tue, Mar 6, 2012 at 3:07 PM, kumar s <[hidden email]> 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  -  [hidden email]
>>> 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
>
> Here is a shorter way:
>
>>>> source = 'ab123t&4533www'
>>>> my_list = re.findall('\d*', source)

if you change above to '\d+' it will only pick up the numbers so below
you can do
>>>> sum(int(s) for s in my_list)

which is even simpler.
>>>> my_list
> ['', '', '123', '', '', '4533', '', '', '', '']
>>>> sum(int(s) for s in my_list if s != '')
> 4656
>

Sorry for my messing up the regex in the first try
>
> --
> Joel Goldstick



--
Joel Goldstick
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor