Re: extracting numbers with decimal places from a string

2015-01-11 Thread Steven D'Aprano
Thomas 'PointedEars' Lahn wrote: > 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”. Yes, a

Re: extracting numbers with decimal places from a string

2015-01-11 Thread Thomas 'PointedEars' Lahn
Mark Lawrence wrote: > On 11/01/2015 23:07, Thomas 'PointedEars' Lahn wrote: >> 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 >> |

Re: extracting numbers with decimal places from a string

2015-01-11 Thread MRAB
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

Re: extracting numbers with decimal places from a string

2015-01-11 Thread Steven D'Aprano
Thomas 'PointedEars' Lahn wrote: > This is safer: > > | >>> from re import split > | >>> split(r'\s*,\s*', '1.23, 2.4, 3.123') > | ['1.23', '2.4', '3.123'] Safer, slower, and unnecessary. There is no need for the nuclear-powered bulldozer of regular expressions just to crack this tiny peanut. W

Re: extracting numbers with decimal places from a string

2015-01-11 Thread Thomas 'PointedEars' Lahn
Peter Otten wrote: > Thomas 'PointedEars' Lahn wrote: >> […] But float() is always necessary for computing the sum and suffices >> indeed together with s.split() if s is just a comma-separated list of >> numeric strings with optional whitespace leading and trailing the comma: >> >> print(sum

Re: extracting numbers with decimal places from a string

2015-01-11 Thread Chris Angelico
On Mon, Jan 12, 2015 at 11:09 AM, Peter Otten <__pete...@web.de> wrote: >> >> print(sum(map(lambda x: float(x), s.split(','))) >> >> Please trim your quotes to the relevant minimum. > > Hm, can you explain what this > > lambda x: float(x) > > is supposed to achieve? I mean other than to confuse a

Re: extracting numbers with decimal places from a string

2015-01-11 Thread Peter Otten
Thomas 'PointedEars' Lahn wrote: > Joel Goldstick wrote: > >> On Sun, Jan 11, 2015 at 6:12 PM, Thomas 'PointedEars' Lahn >> wrote: >>> Joel Goldstick wrote: Am I missing something. >>>^ >>> […] >>> You are missing a leading space character because in the string the >

Re: extracting numbers with decimal places from a string

2015-01-11 Thread Mark Lawrence
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

Re: extracting numbers with decimal places from a string

2015-01-11 Thread Thomas 'PointedEars' Lahn
Joel Goldstick wrote: > On Sun, Jan 11, 2015 at 6:12 PM, Thomas 'PointedEars' Lahn > wrote: >> Joel Goldstick wrote: >>> Am I missing something. >>^ >> […] >> You are missing a leading space character because in the string the comma >> was followed by one. > > I see that

Re: extracting numbers with decimal places from a string

2015-01-11 Thread Joel Goldstick
On Sun, Jan 11, 2015 at 6:12 PM, Thomas 'PointedEars' Lahn wrote: > Joel Goldstick wrote: > >> Thomas 'PointedEars' Lahn wrote: >>> Joel Goldstick wrote: my_list = "1.23, 2.4, 3.123".split(",") that will give you ['1.23', '2.4', '3.123'] >>> >>> No, it gives >>> >>> […] >>> | >>> my

Re: extracting numbers with decimal places from a string

2015-01-11 Thread Thomas 'PointedEars' Lahn
Joel Goldstick wrote: > Thomas 'PointedEars' Lahn wrote: >> Joel Goldstick wrote: >>> my_list = "1.23, 2.4, 3.123".split(",") >>> >>> that will give you ['1.23', '2.4', '3.123'] >> >> No, it gives >> >> […] >> | >>> my_list = "1.23, 2.4, 3.123".split(",") >> | >>> my_list >> | ['1.23', ' 2.4', ' 3

Re: extracting numbers with decimal places from a string

2015-01-11 Thread Grant Edwards
On 2015-01-11, Joel Goldstick wrote: > That's fine, but its different than your original question. In your > original question you had a string of floats separated by commas. To > solve that problem you need to first split the string on the commas: > > my_list = "1.23, 2.4, 3.123".split(",") >

Re: extracting numbers with decimal places from a string

2015-01-11 Thread Thomas 'PointedEars' Lahn
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

Re: extracting numbers with decimal places from a string

2015-01-11 Thread Joel Goldstick
On Sun, Jan 11, 2015 at 5:20 PM, Thomas 'PointedEars' Lahn wrote: > Joel Goldstick wrote: > >> my_list = "1.23, 2.4, 3.123".split(",") >> >> that will give you ['1.23', '2.4', '3.123'] > > No, it gives > > | $ python > | Python 2.7.9 (default, Dec 11 2014, 08:58:12) > | [GCC 4.9.2] on linux2 > | T

Re: extracting numbers with decimal places from a string

2015-01-11 Thread Thomas 'PointedEars' Lahn
Joel Goldstick wrote: > my_list = "1.23, 2.4, 3.123".split(",") > > that will give you ['1.23', '2.4', '3.123'] No, it gives | $ python | Python 2.7.9 (default, Dec 11 2014, 08:58:12) | [GCC 4.9.2] on linux2 | Type "help", "copyright", "credits" or "license" for more information. | >>> my_list

Re: extracting numbers with decimal places from a string

2015-01-11 Thread Joel Goldstick
On Sun, Jan 11, 2015 at 3:26 PM, Store Makhzan wrote: > On Sunday, January 11, 2015 at 2:06:54 PM UTC-6, Joel Goldstick wrote: >> On Sun, Jan 11, 2015 at 2:31 PM, Store Makhzan wrote: >> > I have this script which can calculate the total of numbers given in a >> > string >> > script - >>

Re: extracting numbers with decimal places from a string

2015-01-11 Thread Store Makhzan
On Sunday, January 11, 2015 at 2:06:54 PM UTC-6, Joel Goldstick wrote: > On Sun, Jan 11, 2015 at 2:31 PM, Store Makhzan wrote: > > I have this script which can calculate the total of numbers given in a > > string > > script - > > total = 0 > > for c in '0123456789': > >total += int(c)

Re: extracting numbers with decimal places from a string

2015-01-11 Thread Joel Goldstick
On Sun, Jan 11, 2015 at 2:31 PM, Store Makhzan wrote: > I have this script which can calculate the total of numbers given in a string > script - > total = 0 > for c in '0123456789': >total += int(c) > print total > script - > > How should I modify this script to find the tota

extracting numbers with decimal places from a string

2015-01-11 Thread Store Makhzan
I have this script which can calculate the total of numbers given in a string script - total = 0 for c in '0123456789': total += int(c) print total script - How should I modify this script to find the total of if the numbers given in the string form have decimal places? That