The longest word

NiklasRTZ niklasro at gmail.com
Tue Jul 28 07:55:26 EDT 2009


On Jul 28, 7:03 am, Hrvoje Niksic <hnik... at xemacs.org> wrote:
> Piet van Oostrum <p... at cs.uu.nl> writes:
>
> >>>>>> NiklasRTZ <nikla... at gmail.com> (N) wrote:
>
> >>N> Thank you. This seems to work:
> >>N> sorted("a AAA aa aaaaa  sdfsdfsdfsdf vv".split(' '),lambda a,b: len(a)-
> >>N> len(b))[-1]
> >>N> 'sdfsdfsdfsdf'
>
> > simpler:
>
> > sorted("a AAA aa aaaaa  sdfsdfsdfsdf vv".split(' '), key=len)[-1]
>
> There is no need to sort the sequence to obtain the largest element.
> The max function is designed to do exactly that, and also supports the
> key argument:
>
> >>> max("a AAA aa aaaaa  sdfsdfsdfsdf vv".split(' '), key=len)
>
> 'sdfsdfsdfsdf'

Sincere thanks for strengthening python's superior flexibility. Same
function also works around an exploding index problem returning
results for longest word where otherwise a word with whitespace
crashes the index:
all().search(max(q.split(), key=len)).filter("modified >",
timeline).filter("published =", True).filter("modified <=",
bookmark ).order("-modified").fetch(PAGESIZE+1)
Line below crashes the index for words with whitespace (unknown
researchable, only occurs live, works with development)
all().search(q).filter("modified >", timeline).filter("published =",
True).filter("modified <=", bookmark ).order("-modified").fetch
(PAGESIZE+1)
best regards,
Niklas



More information about the Python-list mailing list