Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python函数:我如何才能正确地完成它?_Python - Fatal编程技术网

Python函数:我如何才能正确地完成它?

Python函数:我如何才能正确地完成它?,python,Python,我一直在努力解决这个问题,但我只做了部分,所以我需要一些帮助,我想知道为什么它不起作用 def hapax_legomena_ratio(text): """ (list of str) -> float Precondition: text is non-empty. Each str in text ends with \n and at least one str in text contains more than just \n. Return

我一直在努力解决这个问题,但我只做了部分,所以我需要一些帮助,我想知道为什么它不起作用

def hapax_legomena_ratio(text):
    """ (list of str) -> float

    Precondition: text is non-empty. Each str in text ends with \n and at
    least one str in text contains more than just \n.

    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
    """
    names = {}
    words = 0
    for line in text():
        line = line.strip().split()
        for word in line:
            words += 1
            word = word.replace(',', '').strip()
            if word in range(len(names)):
                names[word] -= 1
            else:
                names[word] = 1

    name_count = 0
    for each in range(len(names)):
        if names[each] == 1:
            name_count += 1
            result = name_count/words

    return result     
你应该改变

if word in range(len(names)):


您需要进行几个更改。Yayanth已经提出了两项建议

def hapax_legomena_ratio(text):
    """ (list of str) -> float

    Precondition: text is non-empty. Each str in text ends with \n and at
    least one str in text contains more than just \n.

    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
    """
    names = {}
    words = 0
    for line in text:
        line = line.strip().split()
        for word in line:
            words += 1
            word = word.replace(',', '').strip()
            if word in names:
                names[word] += 1
            else:
                names[word] = 1

    name_count = 0
    for name in names:
        count = names[name]
        if count == 1:
            name_count += 1

    result = name_count*1.0/words
    return result

计数时,
collections.Counter
通常很有用:

import collections

def hapax_legomena_ratio(text):
    counter = collections.Counter()
    for line in text:
        counter.update(line.split())
    n_uniques = sum(1 for w in counter if counter[w] == 1)
    return float(n_uniques) / len(counter)
不需要
.strip()
,因为
.split()
会在任何空格上拆分,并且不会生成空字符串。

不确定是否需要
()
之后的
。如果
text
是字符串列表或文件对象,则不需要
()
for each in names:
def hapax_legomena_ratio(text):
    """ (list of str) -> float

    Precondition: text is non-empty. Each str in text ends with \n and at
    least one str in text contains more than just \n.

    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
    """
    names = {}
    words = 0
    for line in text:
        line = line.strip().split()
        for word in line:
            words += 1
            word = word.replace(',', '').strip()
            if word in names:
                names[word] += 1
            else:
                names[word] = 1

    name_count = 0
    for name in names:
        count = names[name]
        if count == 1:
            name_count += 1

    result = name_count*1.0/words
    return result
import collections

def hapax_legomena_ratio(text):
    counter = collections.Counter()
    for line in text:
        counter.update(line.split())
    n_uniques = sum(1 for w in counter if counter[w] == 1)
    return float(n_uniques) / len(counter)