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



示例如何计算段落中的"段落" .

Word中的段落是任何以硬返回结尾的文本。你在按Enter键时插入硬返回。段格式化允许您控制单个段落的外观。例如,您可以更改文本的对齐方式,从左到中或者线之间的间距从单行变成双行。你可以缩进段落,编号,或添加边框和阴影。

段落格式应用于整个段落。所有格式对于一个段落,它存储在段落标记中,并携带到当你按下回车键时。你可以复制段落从段落到段落的格式和通过任务的视图格式窗格。

您要对输入字符串使用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()将文本转换为全小写。这将包括计数中的"段落"one_answers"段落"实例。

我会这样做:

  1. 拆分为单词列表(虽然不是完全必要的)
  2. 小写
  3. 使用count计数
  4. 实例数
>>> 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.nn    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

下面是另一个例子,将段落分割成单词,然后循环遍历单词列表,并在找到目标单词时增加计数器。

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

相关内容

最新更新