Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #83579

Re: extracting numbers with decimal places from a string

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; ';-)': 0.03; 'url:pypi': 0.03; 'from:addr:yahoo.co.uk': 0.04; 'modify': 0.07; 'python3': 0.07; 'string': 0.09; 'lawrence': 0.09; 'line:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:string': 0.09; 'python': 0.11; 'bug': 0.12; 'language.': 0.14; '###': 0.16; '23:07,': 0.16; '4.4,': 0.16; 'comma': 0.16; 'computes': 0.16; 'expressions,': 0.16; 'grasp': 0.16; 'instead:': 0.16; 'iterates': 0.16; 'presented,': 0.16; 'pythonic': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'tracker,': 0.16; 'language': 0.16; 'wrote:': 0.18; 'module': 0.19; '>>>': 0.22; 'import': 0.22; 'print': 0.22; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'integer': 0.24; 'string,': 0.24; 'script': 0.25; 'header:X-Complaints-To:1': 0.27; 'header:In- Reply-To:1': 0.27; 'fixed': 0.29; 'characters': 0.30; 'dec': 0.30; 'code': 0.31; "skip:' 10": 0.31; 'bug?': 0.31; 'decimal': 0.31; 'everywhere': 0.31; 'loads': 0.31; 'subject:numbers': 0.31; 'regular': 0.32; 'linux': 0.33; 'url:python': 0.33; 'bugs': 0.33; 'subject:from': 0.34; 'subject:with': 0.35; "can't": 0.35; 'except': 0.35; 'convert': 0.35; 'but': 0.35; 'there': 0.35; 'url:org': 0.36; 'should': 0.36; 'expected': 0.38; 'handle': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'skip:- 60': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'how': 0.40; 'tell': 0.60; 'numbers': 0.61; 'our': 0.64; 'more': 0.64; 'total': 0.65; 'thomas': 0.65; 'here': 0.66; '2014,': 0.84; 'sum.': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Mark Lawrence <breamoreboy@yahoo.co.uk>
Subject Re: extracting numbers with decimal places from a string
Date Mon, 12 Jan 2015 00:04:49 +0000
References <7240a574-dfd7-46c3-80eb-0657b41894bb@googlegroups.com> <3233082.KAWmNIKqbW@PointedEars.de>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host host-92-24-222-48.ppp.as43234.net
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0
In-Reply-To <3233082.KAWmNIKqbW@PointedEars.de>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.17602.1421021101.18130.python-list@python.org> (permalink)
Lines 82
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1421021101 news.xs4all.nl 2898 [2001:888:2000:d::a6]:37437
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:83579

Show key headers only | View raw


On 11/01/2015 23:07, Thomas 'PointedEars' Lahn wrote:
> Store Makhzan wrote:
>
>> I have this script which can calculate the total of numbers given in a
>> string […]
>> total = 0
>> for c in '0123456789':
>>     total += int(c)
>> print total
>>
>> […]
>> How should I modify this script to find the total of if the numbers given
>> in the string form have decimal places? That is, how do I need to modify
>> this line: […]
>>
>> for c in '1.32, 5.32, 4.4, 3.78':
>>
>> […] to find the total of these given numbers.
>
> The original script already does not do what it advertises.  Instead, it
> iterates over the characters of the string, attempts to convert each to an
> integer and then computes the sum.  That is _not_ “calculate the total of
> numbers given in a string”.
>
> A solution has been presented, but it is not very pythonic because the
> original code was not; that should have been
>
> ### Ahh, Gauß ;-)
> print(sum(map(lambda x: int(x), list('0123456789'))))
> ### --------------------------------------------------------------------
>
> Also, it cannot handle non-numeric strings well.  Consider this instead:
>
> ### --------------------------------------------------------------------
> from re import findall
>
> s = '1.32, 5.32, 4.4, 3.78'
> print(sum(map(lambda x: float(x), findall(r'-?\d+\.\d+', s))))
> ### --------------------------------------------------------------------
>
> But if you are sure that except for the comma separator there are only
> numeric strings, it is more efficient to use re.split() instead of
> re.findall() here.
>
>
> Aside:
>
> I thought I had more than a fair grasp of regular expressions, but I am
> puzzled by
>
> | $ python3
> | Python 3.4.2 (default, Dec 27 2014, 13:16:08)
> | [GCC 4.9.2] on linux
> | >>> from re import findall
> | >>> s = '1.32, 5.32, 4.4, 3.78'
> | >>> findall(r'-?\d+(\.\d+)?', s)
> | ['.32', '.32', '.4', '.78']
>
> Why does this more flexible pattern not work as I expected in Python 3.x,
> but virtually everywhere else?
>
> And why this?
>
> | >>> findall(r'-?\d+\.\d+', s)
> | ['1.32', '5.32', '4.4', '3.78']
> | >>> findall(r'-?\d+(\.\d+)', s)
> | ['.32', '.32', '.4', '.78']
>
> Feature?  Bug?
>

I can't tell you as I avoid regexes like I avoid the plague.  Having 
said that I do know that there loads of old bugs on the bug tracker, 
many of which are fixed in the "new" regex module that's available here 
https://pypi.python.org/pypi/regex/

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

extracting numbers with decimal places from a string Store Makhzan <store0a@gmail.com> - 2015-01-11 11:31 -0800
  Re: extracting numbers with decimal places from a string Joel Goldstick <joel.goldstick@gmail.com> - 2015-01-11 15:06 -0500
    Re: extracting numbers with decimal places from a string Store Makhzan <store0a@gmail.com> - 2015-01-11 12:26 -0800
      Re: extracting numbers with decimal places from a string Joel Goldstick <joel.goldstick@gmail.com> - 2015-01-11 16:42 -0500
        Re: extracting numbers with decimal places from a string Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-11 23:20 +0100
          Re: extracting numbers with decimal places from a string Joel Goldstick <joel.goldstick@gmail.com> - 2015-01-11 17:54 -0500
            Re: extracting numbers with decimal places from a string Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-12 00:12 +0100
              Re: extracting numbers with decimal places from a string Joel Goldstick <joel.goldstick@gmail.com> - 2015-01-11 18:35 -0500
                Re: extracting numbers with decimal places from a string Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-12 00:46 +0100
                Re: extracting numbers with decimal places from a string Peter Otten <__peter__@web.de> - 2015-01-12 01:09 +0100
                Re: extracting numbers with decimal places from a string Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-12 01:45 +0100
                Re: extracting numbers with decimal places from a string Chris Angelico <rosuav@gmail.com> - 2015-01-12 11:17 +1100
          Re: extracting numbers with decimal places from a string Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-01-12 11:45 +1100
        Re: extracting numbers with decimal places from a string Grant Edwards <invalid@invalid.invalid> - 2015-01-11 23:06 +0000
  Re: extracting numbers with decimal places from a string Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-12 00:07 +0100
    Re: extracting numbers with decimal places from a string Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-01-12 00:04 +0000
      Re: extracting numbers with decimal places from a string Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-12 02:00 +0100
    Re: extracting numbers with decimal places from a string MRAB <python@mrabarnett.plus.com> - 2015-01-12 00:59 +0000
    Re: extracting numbers with decimal places from a string Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-01-12 12:13 +1100

csiph-web