Python程序计算给定文本中单词的出现

Given a text (paragraph) and a word whose occurrence to be found in the text/paragraph, we have to find the how many times word is repeated in the text.

给定一个文本 (段落),其出现在文本/段落被找到的单词 ,我们必须找到如何多次在文本重复。

Example:

例:

    Input:
    text = "this is a book, this is very popular"
    word = "this"

    Output:
    'this' found 2 times.

Consider the below program implemented for counting occurrences of just one word in a text.

考虑下面的程序,该程序用于计数文本中仅一个单词的出现

Program:

程序:

# Python program to count occurrence 
# of a word in text

# paragraph
text = """Lorem Ipsum is simply dummy text of the 
printing and typesetting industry. Lorem Ipsum has been 
the industry's standard dummy text ever since the 1500s"""

word = "text"
# searching word
count = 0
for w in text.split():
    if w == word:
        count = count + 1
# printing result
print("\'%s\' found %d times." %(word, count))

word = "is"
# searching word
count = 0
for w in text.split():
    if w == word:
        count = count + 1
# printing result
print("\'%s\' found %d times." %(word, count))

word = "Hello"
# searching word
count = 0
for w in text.split():
    if w == word:
        count = count + 1
# printing result
print("\'%s\' found %d times." %(word, count))

Output

输出量

'text' found 2 times.
'is' found 1 times.
'Hello' found 0 times.


翻译自: https://www.includehelp.com/python/count-occurrence-of-a-word-in-the-given-text.aspx

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值