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/11/24 09:18:37
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模块)会很方便,你确定你是初学者?因为不用正则的话,分词有点麻烦.里面是用逗号和空格分词的.