【发布时间】:2018-03-24 01:49:06
【问题描述】:
我正在尝试创建一个函数来删除标点符号并将字符串中的每个字母小写。然后,它应该以字典的形式返回所有这些,计算字符串中的单词频率。
这是我目前写的代码:
def word_dic(string):
string = string.lower()
new_string = string.split(' ')
result = {}
for key in new_string:
if key in result:
result[key] += 1
else:
result[key] = 1
for c in result:
"".join([ c if not c.isalpha() else "" for c in result])
return result
但这是我执行后得到的:
{'am': 3,
'god!': 1,
'god.': 1,
'i': 2,
'i?': 1,
'thanks': 1,
'to': 1,
'who': 2}
我只需要删除单词末尾的标点符号。
【问题讨论】:
标签: python python-3.x function dictionary punctuation