python 统计list中所有str 中的单词长度 计算平均值def avg_word_length(text): """ (list of str) -> float Precondition: text is non-empty. Each str in text ends with \n and text contains at least one word. Return the average l
来源:学生作业帮助网 编辑:作业帮 时间:2024/06/28 10:15:40
![python 统计list中所有str 中的单词长度 计算平均值def avg_word_length(text):](/uploads/image/z/10692934-70-4.jpg?t=python+%E7%BB%9F%E8%AE%A1list%E4%B8%AD%E6%89%80%E6%9C%89str+%E4%B8%AD%E7%9A%84%E5%8D%95%E8%AF%8D%E9%95%BF%E5%BA%A6+%E8%AE%A1%E7%AE%97%E5%B9%B3%E5%9D%87%E5%80%BCdef+avg_word_length%28text%29%3A++++%22%22%22+%28list+of+str%29+-%3E+float++++Precondition%3A+text+is+non-empty.+Each+str+in+text+ends+with+%5Cn+and++++text+contains+at+least+one+word.++++Return+the+average+l)
python 统计list中所有str 中的单词长度 计算平均值def avg_word_length(text): """ (list of str) -> float Precondition: text is non-empty. Each str in text ends with \n and text contains at least one word. Return the average l
python 统计list中所有str 中的单词长度 计算平均值
def avg_word_length(text):
""" (list of str) -> float
Precondition: text is non-empty. Each str in text ends with \n and
text contains at least one word.
Return the average length of all words in text.
>>> text = ['James Fennimore Cooper\n', 'Peter, Paul and Mary\n']
>>> avg_word_length(text)
5.142857142857143
"""
i = 0
n = 0
w = 0
for i in range(len(text)):
for word in text[i]:
for ch in word:
if ch != '':
n += 1
w += 1
return n / w
我的body是错的.return 1.0..求大神帮帮忙啊!
python 统计list中所有str 中的单词长度 计算平均值def avg_word_length(text): """ (list of str) -> float Precondition: text is non-empty. Each str in text ends with \n and text contains at least one word. Return the average l
需要分词,用正则(re模块)会很方便,你确定你是初学者?因为不用正则的话,分词有点麻烦.里面是用逗号和空格分词的.