def word_count (x: str) -> str:
characters = len(x)
word = len(x.split())
average = sum(len(x) for x in word)/len(word)
print('Characters: ' + str(char) + '\n' + 'Words: ' + str(word) + '\n' + 'Avg word length: ' + str(avg) + '\n')
这段代码对于普通字符串运行正常,但对于以下这种字符串:
'***The ?! quick brown cat: leaps over the sad boy.'
如何编辑代码,使得像“***”和“?!”这样的符号不被计入代码中? 上面句子的平均单词数应该是3.888889,但我的代码给出了另一个数字。
re
过滤掉不需要的内容是实现这一目标的相对简单的方法(例如双空格、特殊字符等)。 - Demian Brecht