列出对平均值的理解

时间:2012-10-23 04:48:14

标签: python string list dictionary average

我有一个字符串,字典:

('The puppy likes flowers',
 {'laughter': (8.5, 0.9313),
  'flowers': (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),
  'likes':(5.9, 0.032),
  'like':(6.5, 0.021)    
   }
  )

每个括号都是一个元组,对应于(得分,标准偏差)。我取每个元组中第一个整数的平均值。我试过这个:

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 ('The puppy likes 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)})

我应该只获得'the''puppy', 'likes''flowers' 4.98 + 7.88 + 5.9 + 7.58 / 4的平均值,但此运行功能还包括'like''flow'4.98 + 7.88 + 5.9 + + 7.58 + 6.5 + 2.3 / 6

5 个答案:

答案 0 :(得分:2)

首先使用变量字符串并不是一个好主意...但是它可以在这里...你在逻辑上有一个缺陷......以下工作

def avg(l):
    if l:
        return sum(l)/len(l)
    return 0

def score(s, d):
    return avg([d.get(x,[0])[0] for x in s.lower().split()])

这将为不在s中的字符串d添加0 ...如果您想忽略它们,请使用以下代码

def score(s, d):
    return avg([d[x][0] for x in s.lower().split() if x in d])

答案 1 :(得分:0)

您应首先拆分字符串:

splited_string = string.split()
included = [d[word][0]for word in d if word in splited_string]

答案 2 :(得分:0)

你可以在下面的函数中得到这个部分,但我决定稍微清理你的元组:

tuple = ('The puppy likes flowers',
 {'laughter': (8.5, 0.9313),
  'flowers': (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),
  'likes':(5.9, 0.032),
  'like':(6.5, 0.021)    
   }
  )

string = tuple[0]
dict = tuple[1]

现在定义我们的功能:

def score(string, dict):
    s = 0
    n = 0
    for each in string.lower().split(' '):
       if each in dict.keys():
          s += dict[each][0]
          n += 1
    average = s/n
    return average

在你的情况下:

In [43]: string
Out[43]: 'The puppy likes flowers'

In [44]: dict
Out[44]: 
{'died': (1.56, 1.198),
 'flow': (2.3, 0.51),
 'flowers': (7.88, 1.1718),
 'laugh': (9.5, 0.1),
 'laughter': (8.5, 0.9313),
 'like': (6.5, 0.021),
 'likes': (5.9, 0.032),
 'puppy': (7.58, 1.4581),
 'the': (4.98, 0.9145)}

评估功能:

In [45]: score(string, dict)
Out[45]: 6.585

答案 3 :(得分:0)

不要使用python的'in'操作,而是尝试使用== 那是, 编辑:

string = string.split(' ') #Returns a list of word

included = [d[word][0]for word in d if word == string]

答案 4 :(得分:0)

与目前为止的其他答案一样,这个答案在字典中查找从输入字符串中拆分的单词的分数,这与您的示例代码所做的不同,即查找字典单词作为输入字符串的一部分,加上他们的分数。此外,这个答案的逻辑类似于其他一些答案的逻辑,但通过使用python的内置filter函数表达得更紧凑。以下所示程序的输出为6.5856.15333333333None6.032四行。

w={'puppy': (7.58, 1.4581), 'likes': (5.9, 0.032), 'laugh': (9.5, 0.1), 'flow': (2.3, 0.51), 'the': (4.98, 0.9145), 'flowers': (7.88, 1.1718), 'laughter': (8.5, 0.9313), 'died': (1.56, 1.198), 'like': (6.5, 0.021)}

def score(s, d):
    v = [d[a][0] for a in filter(lambda x: x in d, s.lower().split())]
    return sum(v)/len(v) if len(v) else None

print score('the puppy likes flowers', w)
print score('the puppy likes flower', w)
print score('short stuff', w)
print score('the flowers flow like laughter', w)