python中,怎么做个字典,数句子中单词出

如题所述

def get_words(sentence):
return sentence.split(" ")

def get_word(word):
if word.isalpha():
return word
else:
return word[:-1]

if __name__ == "__main__":
test = "this is a test, thanks! this is another test, thanks!"
test_words = get_words(test)

test_words_clear = list()
for word in test_words:
test_words_clear.append(get_word(word))

result = dict()
for word in test_words_clear:
if result.get(word, None) is None:
result[word] = 1
else:
result[word] += 1

print(result)
温馨提示:答案为网友推荐,仅供参考
相似回答