Fate of lambda, Functional Programming in Python...
John Roth
newsgroups at jhrothjr.com
Sat Aug 21 09:22:18 EDT 2004
"Reinhold Birkenfeld" <reinhold-birkenfeld-nospam at wolke7.net> wrote in
message news:2on5k1FcjmvdU1 at uni-berlin.de...
> Jeff Sandys wrote:
>
> > | > > Hi,all
> > | > > I have a list like [(id,string),...],for example:
> > | > >
> > | > > [(1,'xxxxx'),(7,'ppppp'),(4,'gggggg'),...]
> > | > >
> > | > > I want to sort this list according to the id of each element.
> > | > > After sorting,the list will become:
> > | > >
> > | > > [(1,'xxxxx'),(4,'gggggg'),(7,'ppppp')...]
> > | >
> > | > list.sort sorts tuples by first argument, then second &c. If you
> > want
> > | > a custom sort order, look at the decorate-sort-undecorate pattern.
> > |
> > | Or if you want to sort only on your Id, use a lambda:
> > | l.sort(lambda x,y: cmp(x[0],y[0]))
> > |
> > | To force sorting on just the nth element of the tuples, replace 0
> > with
> > | n in the above.
> > |
> >
> > I think that lambda should be unlimited and expanded. It already has
> > the
> > colon, block delimiter, so allow lambda to be a multistatement and
> > multiline
> > indented block!
>
> So in what point exactly is this different from a def(), then?
def() is a statement. It can't be used in an expression. That's
the crux of the problem, and always has been the crux of
the problem.
John Roth
> Reinhold
More information about the Python-list
mailing list