how to sorted by summed itemgetter(x)
Paul Rubin
http
Sun Jun 24 21:19:11 EDT 2007
Aldarion <ErendisAldarion at gmail.com> writes:
> how to sorted by summed itemgetter(1)?
> maybe sorted(items,key = lambda x:sum(x[1]))
> can't itemgetter be used here?
You really want function composition, e.g.
sorted(items, key=sum*itemgetter(1))
where * is a composition operator (doesn't exist in Python).
You could write:
def compose(f,g):
return lambda *a,**k: f(g(*a,**k))
and then use
sorted(items, key=compose(sum,itemgetter(1)))
or spell it out inline:
sorted(items, key=lambda x: sum(itemgetter(1)(x)))
I'd probably do something like:
snd = itemgetter(1) # I use this all the time
sorted(items, key=lambda x: sum(snd(x)))
More information about the Python-list
mailing list