how to sorted by summed itemgetter(x)

Aldarion ErendisAldarion at gmail.com
Mon Jun 25 00:01:37 EDT 2007


Thanks for the reply,I got it.
On 6 25 ,   9 19 , Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
> Aldarion <ErendisAldar... 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