2012-10-23 134 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)