views:

567

answers:

2

My data

466.67
465.56
464.44
463.33
462.22
461.11
460.00
458.89
...

I run in Python

sum(/tmp/1,0)

I get an error.

How can you calculate the sum of the values by Python?

+13  A: 
f=open('/tmp/1')
print sum(map(float,f))
Unknown
Please, see my reply to your initial answer.
Masi
Why isn't this using the sum method like SilentGhost's answer?
job
+1 for using map() on a file: clever!
EOL
Why is this question more Pythonic than SilentGhost's one? --- In my opinion, the use of the function `map` is rather implicit.
Masi
@Masi: whom do you ask? **you** accepted this answer!
SilentGhost
@ I ask everybody. --- I tend to accept answers which get the vote of the majority.
Masi
well, apparently it's not the best strategy. Accepted answers regardless of their correctness tend to get higher vote. Seriously, how many of the voters saw that tiny `pythonic` tag do you think?
SilentGhost
+9  A: 
sum(float(i) for i in open('/tmp/1.0'))
SilentGhost
Why do you use `.read().split()` in your answer? --- Your code works without them.
Masi
How do you read the part `float(i) for`. This is my first time that I see a function before a for -loop.
Masi
what do you mean how I read it? how I pronounce it?
SilentGhost
How did you deduce that you need to have `float(i) before the for -loop?
Masi
that's just syntax
SilentGhost
@ SilentGhost: Where is the explanation of the syntax in manual? --- I could not find it in Python's help(), nor in the online manual.
Masi
http://docs.python.org/glossary.html#term-generator-expression
SilentGhost
I accept this answer because I see that it is less implicit to use `float(i)` inside `sum` than the use of `map`.
Masi
@SilentGhost: Thank you for the link and your answer! --- I see that your way of using `generator expression` is much clearer than the use of `map` -function.
Masi