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,但是我的代码给了我另一个数字。
答案 0 :(得分:2)
字符串有.translate()
方法可用于此(如果您知道要删除的所有字符):
>>> "***foo ?! bar".translate(None, "*?!")
'foo bar'
答案 1 :(得分:1)
试试这个:
import re
def avrg_count(x):
total_chars = len(re.sub(r'[^a-zA-Z0-9]', '', x))
num_words = len(re.sub(r'[^a-zA-Z0-9 ]', '', x).split())
print "Characters:{0}\nWords:{1}\nAverage word length: {2}".format(total_chars, num_words, total_chars/float(num_words))
phrase = '***The ?! quick brown cat: leaps over the sad boy.'
avrg_count(phrase)
输出:
Characters:34
Words:9
Average word length: 3.77777777778
答案 2 :(得分:0)
您应该能够修剪每个单词中的所有非字母数字字符,然后仅在长度仍然大于0时使用该单词。我找到的第一个解决方案是正则表达式解决方案,但您可能能够找到其他方法来完成它。
Stripping everything but alphanumeric chars from a string in Python
答案 3 :(得分:0)
import re
full_sent = '***The ?! quick brown cat: leaps over the sad boy.'
alpha_sent = re.findall(r'\w+',full_sent)
print(alpha_sent)
将输出:
['The', 'quick', 'brown', 'cat', 'leaps', 'over', 'the', 'sad', 'boy']
要获得平均值,您可以这样做:
average = sum(len(word) for word in alpha_sent)/len(alpha_sent)
将给出:3.77