Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我陷入了这个想法:如何获得句子中第二长的单词?我将在我的代码中将它用于退出路径,其中最长的单词可能无法通过测试。有任何想法吗 ?提前致谢。
像这样的东西:
second_longest = sorted(sentence.split(), key=len)[-2]
然而,这是一个非常幼稚的单词定义,因为它只在空格上分割,所以任何标点符号都将作为单词的一部分包含在内。您可能希望先过滤句子以删除标点符号。