extracting numbers with decimal places from a string
Thomas 'PointedEars' Lahn
PointedEars at web.de
Sun Jan 11 18:46:07 EST 2015
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.
--
PointedEars
Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.
More information about the Python-list
mailing list