用python统计list中只出现一次的单词的比例 (call了一个clean_up method 去掉了后面的\n)def hapax_legomena_ratio(text):""" (list of str) -> floatPrecondition:text is non-empty.Each str in text ends with \n andtext contains at least

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/14 09:14:47
用python统计list中只出现一次的单词的比例 (call了一个clean_up method 去掉了后面的\n)def hapax_legomena_ratio(text): floatPrecondition:text is non-empty.Each str in text ends with \n andtext contains at least" />

用python统计list中只出现一次的单词的比例 (call了一个clean_up method 去掉了后面的\n)def hapax_legomena_ratio(text):""" (list of str) -> floatPrecondition:text is non-empty.Each str in text ends with \n andtext contains at least
用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不会写.
没学过正则

用python统计list中只出现一次的单词的比例 (call了一个clean_up method 去掉了后面的\n)def hapax_legomena_ratio(text):""" (list of str) -> floatPrecondition:text is non-empty.Each str in text ends with \n andtext contains at least
不清楚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 语句用来计算只出现一次的单词个数 (至少出现一次个数 - 至少出现两次个数 = 只出现一次个数)和总单词数的比率.

用python统计list中只出现一次的单词的比例 (call了一个clean_up method 去掉了后面的 )def hapax_legomena_ratio(text): (list of str) -> floatPrecondition:text is non-empty.Each str in text ends with andtext contains at least python中关于list的reference给我讲一下,reference“引用”. python 如何把list中元素一个对一个的换成对应的数字 list = ['abc', 'def', 'ghi']换成 list = ['101', '102', '103'] 以此类推 怎么用 函数统计同一列中 出现的名字的个数(重复出现计一次)? 用python 列出文章中使用最频繁的诗歌单词,并给出它们的出现次数 在一个有序数列中某些数可能不只出现一次,查找时要将所有位置都查到并统计出现的次数,用顺序查找法完成 在电子表格中,要统计一个数据在某一列中出现的重复次数,应该用什么公式?(说明:AB和BA只能算一次) python 统计list中所有str 中的单词长度 计算平均值def avg_word_length(text): (list of str) -> float Precondition: text is non-empty. Each str in text ends with and text contains at least one word. Return the average l python中如何统计两个字典中相同单词数量有多少个? C语言 统计范围内数字出现个数Problem Description请统计某个给定范围【L,R】的所有整数中,数字X出现的次数比如给定范围【2,22】,数字2在数2中出现了一次,在数12中出现了一次,在数20中出现了1次 统计一篇文章中某种词性出现的频率应该用什么软件?要统计的文章是中文! python括号区别python中,花括号,中括号,小括号的区别在哪里 python 统计单词平均长度,统计a出现的次数有一个txt文件叫123.txt,里面是很多很多单词我想统计这些单词的平均长度,然后找出a出现的次数请根据这个框架写code,包括之前怎样打开那个txt文件def EXCEL如何实现同一列中重复项每出现一次统计次数加1? python中return和yield怎么用的?两个有什么区别? python中readline()怎么用,还有readline和readlines,read的区别和用法a=['dewe','dsefwe','seww']>>> print(a.readline())Traceback (most recent call last):File ,line 1,in print(a.readline())AttributeError:'list' object has no attribute 'r Excel中如何统计某个数出现的次数 excel中可不可以统计某个词的出现次数?