[英]Finding average word length in a string
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,但是我的代码给了我另一个数字。
字符串有一个.translate()
方法,您可以使用它(如果您知道要删除的所有字符):
>>> "***foo ?! bar".translate(None, "*?!")
'foo bar'
试试这个:
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
您应该能够修剪每个单词中的所有非字母数字字符,然后仅在长度仍然大于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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.