extracting numbers with decimal places from a string

Peter Otten __peter__ at web.de
Mon Jan 12 01:09:21 CET 2015


Thomas 'PointedEars' Lahn wrote:

> Joel Goldstick wrote:
> 
>> On Sun, Jan 11, 2015 at 6:12 PM, Thomas 'PointedEars' Lahn
>> <PointedEars at web.de> 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 now.  Performing float on each element of the list will
>> take care of that, or I guess .strip() on each first.
> 
> As I showed, .strip() is unnecessary.  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(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 newbie...





More information about the Python-list mailing list