Python函数:我该怎么做?

时间:2014-03-07 05:50:08

标签: 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 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     

3 个答案:

答案 0 :(得分:1)

你应该改变

if word in range(len(names)):

if word in names:

for each in range(len(names)):

for each in names:

答案 1 :(得分:0)

您需要进行一些更改。 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

答案 2 :(得分:0)

对于计算事物,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()会在任何空格上拆分而不会产生空字符串。