2015-04-02 16 views
1
def value(x): 

s = "Since 2012, Champion Data has boxed players in five categories, ranging from 'elite' to 'poor', based on a complicated formula assessing their most recent form. Players who have yet to debut, played only sparingly or are returning from long-term injuries (less than 10 games in two seasons) are placed in a sixth category named, appropriately, '?'. " 

目的是通過出現的次數來總結不同出現在文本中的參數x的的索引,和除法,進一步除以的長度內文本。 我嘗試過無數次,但似乎無法弄清楚。的Python - 爲特定的字符工作的字符串

+0

才能添加期望的輸出也 – 2015-04-02 12:40:31

+0

預期產出將是從0-1指示參數x – James 2015-04-02 12:43:50

回答

3

您可以使用枚舉來得到這樣的指標:

indices = [i for i, j in enumerate(s) if x == j]

你可以找到索引的字符串的總和爲sum(indices),爲len(indices)發生的次數和長度爲len(s)

你最終結果可以計算爲

return sum(indices)/(len(indices)*len(s))

+0

該解決方案不會與工作分配一個號碼' 'len(x)> 1''。 – astynax 2015-04-02 13:08:34

+0

它與len(x)=大約500 – James 2015-04-02 13:22:07

+0

''x''(子串),而不是''s''(全文) – astynax 2015-04-02 13:23:57

3

這裏是你的代碼:

import re 

def count(sentence, st): 
    indicies = [i.start() for i in re.finditer(st, sentence)] 
    indicies_sum = sum(indicies) 
    number_of_occurrences = len(indicies) 
    text_len = len(sentence) 
    return float(indicies_sum)/(number_of_occurrences*text_len) 
+0

這隻發現'st'的非重疊事件。這可能也可能不重要,因爲原始文章沒有說明當st可以重疊時應該發生什麼。 – Duncan 2015-04-02 13:46:15

1
def count(s, x): 
    lx = len(x) 
    slices = s.split(x) 
    positions = [len(slice) + lx * n 
       for n, slice in enumerate(slices)] 
    return float(sum(positions))/len(s)/len(slices - 1)