计算段落字符串中单词出现次数的最简单方法

时间:2014-07-15 22:22:47

标签: python string

示例如何计算下面段落中的“段落”一词。

  

Word中的段落是以硬回车结束的任何文本。您   只需按Enter键即可插入硬返回。段   格式化可让您控制各个段落的外观。   例如,您可以更改文本从左到中的对齐方式   或者线之间的间距从单一到双。你可以缩进   段落,编号,或添加边框和阴影。

     

段落格式应用于整个段落。所有格式   对于一个段落存储在段落标记中并传送到   按Enter键时的下一段。你可以复制段落   从段落到段落的格式和通过任务查看格式   窗格。

3 个答案:

答案 0 :(得分:2)

您希望在输入字符串上使用count方法,并将“paragraph”作为参数传递。

>>> text = """A paragraph in Word is any text that ends with a hard return. You insert a hard return anytime you press the Enter key. Paragraph formatting lets you control the appearance if individual paragraphs. For example, you can change the alignment of text from left to center or the spacing between lines form single to double. You can indent paragraphs, number them, or add borders and shading to them.
    Paragraph formatting is applied to an entire paragraph. All formatting for a paragraph is stored in the paragraph mark and carried to the next paragraph when you press the Enter key. You can copy paragraph formats from paragraph to paragraph and view formats through task panes."""

>>> text.count('paragraph') # case sensitive
10
>>> text.lower().count('paragraph') # case insensitive
12

如评论中所述,您可以使用lower()将文本转换为全小写。这将包括计数中“段落”和“段落”的实例。

答案 1 :(得分:0)

我会做以下事情:

  
      
  1. 分成一个单词列表(虽然不是完全必要的)
  2.   
  3. 小写所有单词
  4.   
  5. 使用count计算实例数
  6.   
>>> s
'A paragraph in Word is any text that ends with a hard return. You insert a hard return anytime you press the Enter key. Paragraph formatting lets you control the appearance if individual paragraphs. For example, you can change the alignment of text from left to center or the spacing between lines form single to double. You can indent paragraphs, number them, or add borders and shading to them.\n\n    Paragraph formatting is applied to an entire paragraph. All formatting for a paragraph is stored in the paragraph mark and carried to the next paragraph when you press the Enter key. You can copy paragraph formats from paragraph to paragraph and view formats through task panes.'
>>> s.split()
['A', 'paragraph', 'in', 'Word', 'is', 'any', 'text', 'that', 'ends', 'with', 'a', 'hard', 'return.', 'You', 'insert', 'a', 'hard', 'return', 'anytime', 'you', 'press', 'the', 'Enter', 'key.', 'Paragraph', 'formatting', 'lets', 'you', 'control', 'the', 'appearance', 'if', 'individual', 'paragraphs.', 'For', 'example,', 'you', 'can', 'change', 'the', 'alignment', 'of', 'text', 'from', 'left', 'to', 'center', 'or', 'the', 'spacing', 'between', 'lines', 'form', 'single', 'to', 'double.', 'You', 'can', 'indent', 'paragraphs,', 'number', 'them,', 'or', 'add', 'borders', 'and', 'shading', 'to', 'them.', 'Paragraph', 'formatting', 'is', 'applied', 'to', 'an', 'entire', 'paragraph.', 'All', 'formatting', 'for', 'a', 'paragraph', 'is', 'stored', 'in', 'the', 'paragraph', 'mark', 'and', 'carried', 'to', 'the', 'next', 'paragraph', 'when', 'you', 'press', 'the', 'Enter', 'key.', 'You', 'can', 'copy', 'paragraph', 'formats', 'from', 'paragraph', 'to', 'paragraph', 'and', 'view', 'formats', 'through', 'task', 'panes.']
>>> [word.lower() for word in s.split()].count("paragraph")
9

答案 2 :(得分:0)

这是另一个将段落拆分为单词然后循环遍历单词列表并在找到目标单词时递增计数器的示例。

paragraph = '''insert paragraph here'''
 wordlist = paragraph.split(" ")
 count = 0
 for word in wordlist:
     if word == "paragraph":
         count += 1