我有一个字符串" Hello I am going to I with hello am".我想找出一个单词在字符串中出现的次数.示例hello发生2次.我试过这种方法只打印字符 -
def countWord(input_string):
d = {}
for word in input_string:
try:
d[word] += 1
except:
d[word] = 1
for k in d.keys():
print "%s: %d" % (k, d[k])
print countWord("Hello I am going to I with Hello am")
Run Code Online (Sandbox Code Playgroud)
我想学习如何找到字数.
python ×1