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 送给学长毕业祝福语简短