extracting numbers with decimal places from a string

Thomas 'PointedEars' Lahn PointedEars at web.de
Sun Jan 11 19:45:16 EST 2015


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(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...

 print(sum(map(float, s.split(','))))
suffices in this case, of course.
I could have done without the snide remarks.
-- 
PointedEars
Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /