tags:

views:

133

answers:

4
file = [float(line.partition(' ')[0]) for line in file]

the file is object of open file...

thnq

+1  A: 

For measurement of speed, see the Python standard library module timeit:

$ python -m timeit -s 'f = file("/tmp/numbers.txt")' '[float(line.partition(" ")[0]) for line in f]'
10000000 loops, best of 3: 0.123 usec per loop
$ python -m timeit -s 'f = file("/tmp/numbers.txt")' '[float(line.split(" ")[0]) for line in f]'
10000000 loops, best of 3: 0.132 usec per loop
$ python -m timeit -s 'f = file("/tmp/numbers.txt")' '[float(line.split(" ", 1)[0]) for line in f]'
10000000 loops, best of 3: 0.127 usec per loop

partition seems to be faster than split, at least. I can't think of a faster way right now, so well done.

Lars Wirzenius
what is it with using `file` to open a file?
SilentGhost
It's not mentioned anywhere on the question that this is Python 3 code (as I figured out it was, from one of your comments on another answer). Therefore, Lars clearly assumed it was Python 2.x (where file is perfectly valid, if perhaps undesirable).
rbp
+2  A: 

Smarter would be to not shadow file.

Ignacio Vazquez-Abrams
this code was written in py3k that doesn't have `file`
SilentGhost
Perhaps what he means is that the OP is shadowing his own "object of open file", by assining the resulting list to it (file = [something for line in file]). It might not be relevant in this code, but it's certainly not advisable. Of course, that's not an answer to the question, either.
rbp
+1  A: 

It depends what you are going to do with the list once it has been created. If you are just going to iterate through it then it may be better to use a generator expression so that you do not load the entire file into memory at once. If the file is large this could result in page swapping or out of memory errors.

I have read through the related questions you posted, and can see no information on what problem you are trying to solve or what you are going to do with the data once you have read it. If you give us some more context then we may be able to give more specific and helpful answers.

Dave Kirby
+1  A: 

If you're only interested in everything that comes before the first space (and assuming there always is one), you can try using string index:

[float(line[:line.index(" ")]) for line in f]

Borrowing Lars's tests, it runs faster than partition:

rbp@apfelstrudel ~$ python -m timeit -s 'f = open("/tmp/numbers.txt")' '[float(line.partition(" ")[0]) for line in f]'
10000000 loops, best of 3: 0.192 usec per loop
rbp@apfelstrudel ~$ python -m timeit -s 'f = open("/tmp/numbers.txt")' '[float(line[:line.index(" ")]) for line in f]'
10000000 loops, best of 3: 0.181 usec per loop

Also, of course, if you exchange the outer square brackets for parenthesis, you'll get a generator expression, which doesn't generate all the results straight away. Depending on how you'll use this, it may fit into the "smarter" category :)

Edited to add:

... Although, since SilentGhost mentioned this is py3k, the speed difference is not relevant then:

rbp@apfelstrudel ~$ python3 -m timeit -s 'f = open("/tmp/numbers.txt")' '[float(line[:line.index(" ")]) for line in f]'
100000 loops, best of 3: 10.9 usec per loop
rbp@apfelstrudel ~$ python3 -m timeit -s 'f = open("/tmp/numbers.txt")' '[float(line.partition(" ")[0]) for line in f]'
100000 loops, best of 3: 11 usec per loop

But I still think index is better, as it clearly shows what you mean (as opposed to partition, which gives you two additional values that you throw away immediately)

rbp
they're exactly the same for me
SilentGhost
I've just added py3k measurements and mentioned that the speed difference doesn't apply to it. Perhaps that's what you're experiencing?
rbp
it's nothing to do with py3k, i've tested with python-2.6 and it gives me the same values.
SilentGhost
Really? With Python 2.6 it consistently gives me better results with index (the times I pasted were obtained after running each of them several times, with similar results). Oh well. As I mention on my last edit, I still think index is preferable to partitio, in this case.
rbp