python中,怎么做个字典,数句子中单词出
1个回答
展开全部
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)
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)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询