假设我们不需要关心大小写,所以我使用了 re.IGNORECASE。
我用正则表达式来解决这个问题。我是这样解决的:
total = 0
for line in alllines:
count = 0
count = len(re.findall(word, line, re.IGNORECASE))
total += count
Run Code Online (Sandbox Code Playgroud)
但我遇到了一个我想知道的新问题。如果我只想要完整的单词怎么办?例如,如果该行包含“helloworld”,那么如果我的术语是“hello”,则不应将其计算在内。但我的方式会算数。有什么建议么?