2013-05-13 71 views

回答

1

你的意思呢?

count = 0 
for i in list: 
    if i == string: 
     count += 1.0 
return count/len(list) 

這是錯誤的。

+0

不,我的意思是一個函數,可以計算互惠排名,如第一個鏈接,如果我輸入函數 - >水果('蘋果'),結果將是1/3的列表1,因爲它只在列表中出現一次。但是對於第二個列表,結果將是1/2,因爲它在列表中出現兩次 – 2013-05-13 10:58:27

+2

@Erika我在維基百科中查找它。它不依賴於序列中正確項目的數量。 http://en.wikipedia.org/wiki/Mean_reciprocal_rank – Joonazan 2013-05-13 11:06:59

+0

啊耶!我的不好:(這取決於排名以及.. – 2013-05-13 11:58:22

1
len(filter(lambda x: x == 'apple', list1))/float(len(list1)) 

sum(map(lambda x: x == 'apple', list1))/float(len(list1)) 

reduce(lambda x, y: x + (y == 'apple'), list1, 0.0)/len(list1) 
2

你可以使用collections.Counter

>>> from collections import Counter 
>>> c1 = Counter(list1) 
>>> c2 = Counter(list2) 
>>> def rec_rank(key,dic): 
...  return dic[key]/float(sum(dic.values())) 
... 
>>> rec_rank('apple',c1) 
0.3333333333333333 
>>> rec_rank('apple',c2) 
0.5