0

My small script returns a comma separated list of 10 floating point values, loops through them, and holds them in variable vals.

I sum these up into new variable 'total' like this:

total = sum(map(float, vals))

Easy enough, BUT oftentimes None occurs as one or more values in the list,

4.234,None,0.2398,None,None,0.0166666666667,None,None,None,None

then I get the ValueError:

ValueError: could not convert string to float: N

How can I convert these None values to 0 so that the values sum up without error?

3
  • I would loop over the list before calling sum and replace None with 0. I wonder if there a better way Commented Jun 25, 2014 at 19:05
  • 3
    Your error message makes it seem like vals is a string, not a list. Is that correct? Commented Jun 25, 2014 at 19:06
  • Python newbie and my understanding still evolving. I believe it is a string. Commented Jun 25, 2014 at 19:25

3 Answers 3

3
total = sum(map(float, filter(None,vals)))

From the docs:

Note that filter(function, iterable) is equivalent to [item for item in iterable if function(item)] if function is not None and [item for item in iterable if item] if function is None.

If vals is a string literal (as DSM hints it looks like it is) use can use the ast library

>>> vals = '[4.234,None,0.2398,None,None,0.0166666666667,None,None,None,None]'
>>> import ast
>>> vals_list = ast.literal_eval(vals)
>>> vals_list
[4.234, None, 0.2398, None, None, 0.0166666666667, None, None, None, None]

Then you can simply apply sum and filter

>>> total = sum(filter(None,vals_list))
>>> total
4.4904666666667

Note -- I did append square brackets to the string, so if they are missing that would be a step you would need

Sign up to request clarification or add additional context in comments.

4 Comments

It's important to note that if item behaves differently from item is not None. Also, the function call will be slightly slower than a generator expression.
Sure - if the case is more complex than stated above, then there would probably need to be more pruning
Thanks C.B. I would then have to add code to add brackets to vals, and that seems unnecessary. I liked your line, total = sum(map(float, filter(None,vals))), except I get same error as originally.
@aekandem If you don't go that route, then you will need to use split() and a generator expression that is filtering on the string None, which to me isn't as explicit. You would then have to apply the map(float,iterable) as well. You will have to convert the string into a list one way or another.
1
total = sum(float(val) for val in vals if val is not None)

1 Comment

The [] are unnecessary; using a generator expression instead of a list comprehension will be more efficient.
0
sum(map(lambda x: float(x) if x else 0.0, vals))

1 Comment

Please explain this code, it has been flagged as low quality.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.