用python统计list中只出现一次的单词的比例 (call了一个clean_up method 去掉了后面的\n)
来源:学生作业帮 编辑:神马作文网作业帮 分类:英语作业 时间:2024/09/25 00:42:18
用python统计list中只出现一次的单词的比例 (call了一个clean_up method 去掉了后面的\n)
def hapax_legomena_ratio(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 hapax legomena ratio for text.This ratio is the number of
words that occur exactly once divided by the total number of words.
>>> text = ['James Fennimore Cooper\n','Peter,Paul,and Mary\n',
'James Gosling\n']
>>> hapax_legomena_ratio(text)
0.7777777777777778
"""
t = [ ]
for string in text:
t.append(clean_up(string))
at_least_once = [ ] #规定只能创建两个list来运算 一个是至少出现一次的单词
at_least_twice = [ ] # 这个是至少出现两次的单词
然后应该是利用length做比吧 中间的body不会写.
没学过正则
def hapax_legomena_ratio(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 hapax legomena ratio for text.This ratio is the number of
words that occur exactly once divided by the total number of words.
>>> text = ['James Fennimore Cooper\n','Peter,Paul,and Mary\n',
'James Gosling\n']
>>> hapax_legomena_ratio(text)
0.7777777777777778
"""
t = [ ]
for string in text:
t.append(clean_up(string))
at_least_once = [ ] #规定只能创建两个list来运算 一个是至少出现一次的单词
at_least_twice = [ ] # 这个是至少出现两次的单词
然后应该是利用length做比吧 中间的body不会写.
没学过正则
不清楚clean_up函数做了什么,整个函数都是新写的,你参考一下吧.
def hapax_legomena_ratio(text):
at_least_once = []
at_least_twice = []
total = 0
for s in text:
for word in s.strip().split():
word = word.strip('.,;')
total += 1
if word not in at_least_once:
at_least_once.append(word)
elif word not in at_least_twice:
at_least_twice.append(word)
return (1.0 * (len(at_least_once)-len(at_least_twice)))/total
代码很简单,前面是为了把字符串分割成单词,然后统计单词总数 (total += 1).
后面是核心部分,统计单词是出现了最少一次,还是最少两次.
if word not in at_least_once:
at_least_once.append(word)
elif word not in at_least_twice:
at_least_twice.append(word)
最后的 return 语句用来计算只出现一次的单词个数 (至少出现一次个数 - 至少出现两次个数 = 只出现一次个数)和总单词数的比率.
再问: 谢谢!特别感谢!后面核心的部分懂了 前面单词分割的部分也懂了 可是split的方法还没学过 不可以用的话 要怎么吧字符串分割成单词呢
def hapax_legomena_ratio(text):
at_least_once = []
at_least_twice = []
total = 0
for s in text:
for word in s.strip().split():
word = word.strip('.,;')
total += 1
if word not in at_least_once:
at_least_once.append(word)
elif word not in at_least_twice:
at_least_twice.append(word)
return (1.0 * (len(at_least_once)-len(at_least_twice)))/total
代码很简单,前面是为了把字符串分割成单词,然后统计单词总数 (total += 1).
后面是核心部分,统计单词是出现了最少一次,还是最少两次.
if word not in at_least_once:
at_least_once.append(word)
elif word not in at_least_twice:
at_least_twice.append(word)
最后的 return 语句用来计算只出现一次的单词个数 (至少出现一次个数 - 至少出现两次个数 = 只出现一次个数)和总单词数的比率.
再问: 谢谢!特别感谢!后面核心的部分懂了 前面单词分割的部分也懂了 可是split的方法还没学过 不可以用的话 要怎么吧字符串分割成单词呢
用python统计list中只出现一次的单词的比例 (call了一个clean_up method 去掉了后面的\n)
python 统计单词平均长度,统计a出现的次数
python 如何把list中元素一个对一个的换成对应的数字 list = ['abc', 'def', 'ghi']
PYTHON不能算出小数点后面的位置了!
python 统计list中所有str 中的单词长度 计算平均值
如何在excel里面统计一个表格中相同名字的数量,相同的只显示一个名称后面显示重复数量,去掉重复的名字.
用python 列出文章中使用最频繁的诗歌单词,并给出它们的出现次数
python如何统计列表的长度
请问一下谁知道word中如何统计一个单词出现的频率呢 晓得的人说下哈,打心底谢谢给位朋友了
如何将数据库表中的内容导入到python字典,建了一个表,想把表中的内容放到python的字典中
python如何把List里的不同数字相加成一个数字
python:如何将一个list的第2,5,6,7,8项同时删去?