Python 实现 英语 单词统计 和替换
def count_en(a, word):
index = 0
times = 0
len_str = len(a)
len_word = len(word)
while True:
index = a.find(word, index)
if index == -1:
break
#判断是否为第一个词
if index != 0:
front = a[index-1]
else:
front = ""
#if f is not word it will be my word
back_index = (index + len_word)
#判断是否为最后一个词
if back_index < len_str:
back = a[back_index]
else:
back = ""
index += len_word
if front.isalpha() or back.isalpha():
continue
times+=1
#print times
return times
注释:
这个a是待查的字符,word是文本。这个函数的意义是统计
a出现在word中的次数。
def replace_en(a, word, newword):
index = 0
<