开发者

Is there a way to find the second longest word in a sentence in Python?

I got stuck on this idea: how do I get the second longest word in a sentence ? I'm going to use it for an exit route in my code where the longest w开发者_运维问答ord might fail a test. Any ideas ? Thanks in advance.


something like this:

second_longest = sorted(sentence.split(), key=len)[-2]

This is a pretty naive definition of word however, since it only splits on whitespace so any punctuation will be included as part of the words. You may want to filter the sentence to remove punctuation characters first.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜