0

私は文字列、辞書を次の形式で持っています:

('the head', {'exploded': (3.5, 1.0), 'the': (5.0, 1.0), 
"puppy's": (9.0, 1.0), 'head': (6.0, 1.0)})

各括弧は、(スコア、標準偏差)に対応するタプルです。各タプルの最初の整数の平均をとっています。私はこれを試しました:

def score(string, d):
    for word in d:
        (score, std) = d[word]
        d[word]=float(score),float(std)
        if word in string:
            word = string.lower()
            number = len(string)
            return sum([v[0] for v in d.values()]) / float(len(d))
        if len(string) == 0:
            return 0

私が走るとき:

print score('the head', {'exploded': (3.5, 1.0), 'the': (5.0, 1.0), 
"puppy's": (9.0, 1.0), 'head': (6.0, 1.0)})

取得する必要があります5.5が、代わりに取得してい5.875ます。関数の何が正しい答えを得ることができないのか理解できません。

4

1 に答える 1

1
def score(s, d):
    included = [d[word][0] for word in d if word in s]
    return sum(included) / float(len(included))
于 2012-10-20T22:58:06.457 に答える