[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

about sort a list with integer key

lotrpy

1/13/2008 11:25:00 AM

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 .
3 Answers

Fredrik Lundh

1/13/2008 12:26:00 PM

0

lotrpy wrote:

> 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>

Hrvoje Niksic

1/13/2008 12:33:00 PM

0

lotrpy <lotrpy@gmail.com> writes:

> 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.

lotrpy

1/13/2008 1:45:00 PM

0

On 1?13?, ??8?32?, Hrvoje Niksic <hnik...@xemacs.org> wrote:
> 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.