Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'modify': 0.07; 'python3': 0.07; 'string': 0.09; 'bug.': 0.09; 'lawrence': 0.09; 'line:': 0.09; 'present,': 0.09; 'subject:string': 0.09; 'python': 0.11; 'bug': 0.12; '###': 0.16; '23:07,': 0.16; '4.4,': 0.16; 'comma': 0.16; 'computes': 0.16; 'expressions,': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'grasp': 0.16; 'instead:': 0.16; 'iterates': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'presented,': 0.16; 'pythonic': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'tracker,': 0.16; ':-)': 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; 'instance,': 0.24; 'integer': 0.24; 'string,': 0.24; 'script': 0.25; '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; 'group:': 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; 'received:84': 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; 'how': 0.40; 'tell': 0.60; 'numbers': 0.61; 'entire': 0.61; 'more': 0.64; 'total': 0.65; 'thomas': 0.65; 'here': 0.66; 'groups.': 0.74; '2014,': 0.84; 'sum.': 0.84; 'capture': 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=Pb1xh0Rd c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=xSRs65CQCjkA:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=-8s1hlDDQVn9RiXqQH4A:9 a=Zkj1W_toUHdjVQpM:21 a=4KKT16BoCZyzDUVo:21 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett@:2500 Date: Mon, 12 Jan 2015 00:59:12 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: extracting numbers with decimal places from a string References: <7240a574-dfd7-46c3-80eb-0657b41894bb@googlegroups.com> <3233082.KAWmNIKqbW@PointedEars.de> In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 87 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1421024362 news.xs4all.nl 2894 [2001:888:2000:d::a6]:56297 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:83584 On 2015-01-12 00:04, Mark Lawrence wrote: > 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/ > It's not a bug. re.findall returns the capture groups, if present, or the entire match if there are no capture groups. In this instance, it's better to use a non-capture group: findall(r'-?\d+(?:\.\d+)', s) It's all in the docs! :-)