擅长:python、mysql、java
<p>也许这太过分了,但你可以用正则表达式。为什么?它可能比分裂和循环快一点(并不是说它不会在幕后发生)。享受-我写了一个小片段下面!你知道吗</p>
<pre><code>import re
data = """
I have 12 trampolines. The TRAMPOLINES are round and have netting
surrounding them.
Trampolines are my favorite activity.
"""
def count_instances(data, word):
res = re.findall('.?{}.?'.format(word.lower()), data, flags=re.IGNORECASE)
print(res)
return len(res)
print(count_instances(data, 'trampolines'))
</code></pre>