Python程序计算给定文本中单词的出现
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                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
總結
以上是生活随笔為你收集整理的Python程序计算给定文本中单词的出现的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 数组shift方法_数组shift()方
 - 下一篇: 颐和园几点人少