longest sequence

Martin Miller mmiller at tx3.com
Mon Feb 24 12:57:34 EST 2003


"Mark McEahern" <mark at mceahern.com> wrote, in part:
 >   def longest(*args):
 >       sorted = list(args)
 >       sorted.sort(lambda x, y: cmp(len(x), len(y)))
 >       return sorted[-1]

Although there are a number of faster ways, as described in the other 
posts to this thread, but FWIW, the sort step you have in the above can 
be simplified slightly. From:

     sorted.sort(lambda x, y: cmp(len(x), len(y)))
to:
     sorted.sort(lambda x, y: len(x)-len(y))

This change alone will make it about twice as fast.

mm





More information about the Python-list mailing list