我有一个字符串,字典形式为:
('(Laughter flower)',
{'laughter': (8.5, 0.9313),
'flower': (7.88, 1.1718),
'the': (4.98, 0.9145),
'puppy': (7.58, 1.4581),
'died': (1.56, 1.198),
'laugh': (9.5, 0.1),
'flow': (2.3, 0.51)
}
)
每个括号是对应于(分数,标准差)的元组。我只取每个元组中第一个整数的平均值。我试过这个:
def score(string, d):
if len(string) == 0:
return 0
string = string.lower()
included = [d[word][0]for word in d if word in string]
return sum(included) / len(included)
当我运行时:
print score ('(Laughter flower)', {'laughter': (8.5, 0.9313), 'flower':
(7.88, 1.1718), 'the':(4.98, 0.9145), 'puppy':(7.58, 1.4581),
'died':(1.56, 1.198),'laugh': (9.5, 0.1),'flow': (2.3, 0.51)})
我应该只得到'laughter'
and 'flower'
:8.5 + 7.88 / 2
但这个运行函数还包括'laugh'
and 'flow'
:的平均值8.5 + 7.88 + 9.5 + 2.3 /4
。