about sort a list with integer key

L

lotrpy

hi, if I want sort each line ,by the last part,of a file, below is the
source.
from operator import itemgetter
content = (line.split() for line in file('foo.txt', 'rb'))
for cursor, line in enumerate(sorted(content, key = itemgetter(-1),
reverse = True)):
print cursor, ' '.join(line)
the content of foo.txt is
21 job
3 joke
the result is
0 3 joke
1 21 job

if i want sort each line by the first part,(it's a integer, in fact).
don't know how to do it with itemgetter.
key = int(itemgetter(0)) is wrong, key = lambda x:int(x[0]) works.
but s.b. told me itemgetter execute more quickly .
 
F

Fredrik Lundh

lotrpy said:
key = int(itemgetter(0)) is wrong, key = lambda x:int(x[0]) works.
but s.b. told me itemgetter execute more quickly .

so you're more interested in speed than in correctness? ;-)

operator.itemgetter is a function factory that creates a *function* that
fetches the given item from a sequence. or in other words, typing

func = itemgetter(0)

is pretty much the same thing as typing

def func(seq):
return seq[0]

given this, it should be fairly obvious what int(itemgetter(0)) does: it
attemts to convert the *function* to an integer, which obviously doesn't
work.

I'd stick to the lambda form if I were you. It isn't only easier to
understand for the Python layman, it also does the right thing.

</F>
 
H

Hrvoje Niksic

lotrpy said:
if i want sort each line by the first part,(it's a integer, in fact).
don't know how to do it with itemgetter.
key = int(itemgetter(0)) is wrong, key = lambda x:int(x[0]) works.
but s.b. told me itemgetter execute more quickly .

Use lambda when it works better for you, the speed difference is
marginal in practice anyway. itemgetter is not (and was never
intended to be) a general substitute for functions, as you've
discovered.

The marginal speed difference between itemgetter and an explicit
lambda that does the subscripts is a consequence of itemgetter being
written in C, meaning it avoids the creation of a Python stack frame,
etc. Combining int(...) with this operation requires coding the key
function in Python, which removes itemgetter's advantage. In other
words, you cannot retain itemgetter's speed advantage with more
complex keys. If the sorting performance is a problem for you, please
give more details about what you're doing -- there might be better
ways to speed up the code.
 
L

lotrpy

Use lambda when it works better for you, the speed difference is
marginal in practice anyway. itemgetter is not (and was never
intended to be) a general substitute for functions, as you've
discovered.

The marginal speed difference between itemgetter and an explicit
lambda that does the subscripts is a consequence of itemgetter being
written in C, meaning it avoids the creation of a Python stack frame,
etc. Combining int(...) with this operation requires coding the key
function in Python, which removes itemgetter's advantage. In other
words, you cannot retain itemgetter's speed advantage with more
complex keys. If the sorting performance is a problem for you, please
give more details about what you're doing -- there might be better
ways to speed up the code.

Fredrik and Hrvoje, thanks for the reply, here the sorting performance
is not a big problem for me. the text file is just several hundred
line, each line include several item separated by space. I'll run the
script each week or month, just 1 second to get the result,so maybe
stick to lambda here is just fine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top