Python程序计算给定文本中单词的出现
给定一个文本(段落)和一个字,其出现在文本/段落被发现,我们必须找到多少次字的重复文本。
示例
Input:
text = "this is a book, this is very popular"
word = "this"
Output:
'this' found 2 times.考虑下面的程序,该程序用于计数文本中仅一个单词的出现。
程序:
#Python程序来计数发生
#文字中的一个词
#段
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"
#搜索词
count = 0
for w in text.split():
if w == word:
count = count + 1
#打印结果
print("\'%s\' found %d times." %(word, count))
word = "is"
#搜索词
count = 0
for w in text.split():
if w == word:
count = count + 1
#打印结果
print("\'%s\' found %d times." %(word, count))
word = "Hello"
#搜索词
count = 0
for w in text.split():
if w == word:
count = count + 1
#打印结果
print("\'%s\' found %d times." %(word, count))输出结果
'text' found 2 times. 'is' found 1 times. 'Hello' found 0 times.
热门推荐
10 六一互换礼物祝福语简短
11 舞蹈老师晚安祝福语简短
12 朋友搞笑蛋糕祝福语简短
13 初升高祝福语家长简短
14 妈妈给宝贝祝福语简短
15 公司年会亲属祝福语简短
16 法考面试祝福语简短
17 元旦的祝福语简短 公司
18 生日涂鸦祝福语简短英文