2012-10-23 137 views
0

我有一个字符串,词典形式列表理解:与平均值

('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

+0

有几点需要澄清你的问题[1]你有一个元组,[2]'string'是变量的错误名称(它是内置的名称)和[3]'如果len(string)== 0:'可以简化为'如果不是len(string):' –

+0

你甚至不需要检查长度。 Python会为你做,只需使用如果不是字符串:return None –

+0

类似的帖子:http:// stackoverflow。com/questions/13006271/using-list-comprehension –

回答

2

首先使用变量string是不是一个好主意......但在这里它的好......你在逻辑缺陷...以下工作

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]) 
+0

如果x不在字典中,该怎么办? –

+0

如果x不在d中,则修复此问题。 – Pykler

+1

有趣的方式来解决它。好。 –

0

你应该拆分字符串第一:

splited_string = string.split() 
included = [d[word][0]for word in d if word in splited_string] 
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 
+1

哇在我的答案顶部看到注释...你超级覆盖python builtin变量/类型 – Pykler

+0

我不想改变所有的变量,所以它很容易要理解OP,但是你是对的,它需要有其他变量名。顺便说一句,元组和字典的添加意图是幽默的。 –

0

而不是使用python的“在”操作尝试使用== 。也就是说, 编辑:

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

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

字是字符串的一部分而不是整个事物 – Pykler

+0

另外* in *也不起作用,因为它将允许部分匹配。 – Pykler

+0

感谢您的注意......我完全忘了分割字符串:) –

0

像到目前为止其他的答案,这个答案查找字典中的单词拆分分数来自输入字符串,与您的示例代码不同,它将查找字典单词作为输入字符串的一部分,并将它们的分数相加。此外,这个答案的逻辑类似于其他一些答案的逻辑,但是通过使用python的内置函数filter可以更加简洁地表达。下面显示的程序的输出是四行中的6.585,6.15333333333,None6.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)