longest sequence
Mark McEahern
marklists at mceahern.com
Mon Feb 17 13:34:14 EST 2003
[gustavo]
> Yay, optimizations time! :-)
>
> def longest2(*args):
> if args:
> decorated = [ (len(S),S) for S in args ]
> decorated.sort()
> return decorated[-1][1]
>
> This should be a bit quicker. :-)
Yup, my testing shows your decorate-sort-undecorate approach is twice as
fast. Is the trick here that sort() sorts sequences of tuples on the first
element of each tuple in the sequence?
Thanks!
// m
-
More information about the Python-list
mailing list