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
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
lotrpy [EMAIL PROTECTED] 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
On 1月13日, 下午8时32分, Hrvoje Niksic [EMAIL PROTECTED] 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