user7319004
user7319004

Reputation:

How to convert a list of strings to numbers or float?

I have an output like this:

0.4481696971
0.3993220707
0.4006741959
0.1333734337
0.9441898513

which is stored in a result variable.

What I'm trying to do is calculate the sum of all these lines and divide it by number_lines in this case 5. How could I achieve this?

values = [0.4, 0.0, 1.0, 0.25, 0.90]

print sum(values)

Upvotes: 1

Views: 61

Answers (1)

YLJ
YLJ

Reputation: 2996

(Edited)

You can first split result by \n and convert each of them to float. Then do whatever you want (average).

numbers = list(map(float, result.split("\n")))
print(sum(numbers))  # Sum.
print(sum(numbers) / len(numbers))  # Average.

Upvotes: 1

Related Questions