Python: Finding The Longest/Shortest Sentence In A Random Paragraph?

前端 未结 3 691
感情败类
感情败类 2021-01-27 02:51

I am using Python 2.7 and need 2 functions to find the longest and shortest sentence (in terms of word count) in a random paragraph. For exampl

3条回答
  •  广开言路
    2021-01-27 03:32

    def MaxMinWords(paragraph):
        numWords = [len(sentence.split()) for sentence in paragraph.split('.')]
        return max(numWords), min(numWords)
    

    EDIT : As many have pointed out in the comments, this solution is far from robust. The point of this snippet is to simply serve as a pointer to the OP.

提交回复
热议问题