2016-07-14 32 views
0

我正嘗試使用此代碼來計算特定深度處的重疊部分,以便在各種深度使用。使用for循環來自動執行功能

def score(list1, list2, depth): 
    len_list = len(list1) 

    frac = numpy.ceil(depth * len_list) 

    frac = int(frac) 
    s = slice(0,frac) 

    list1 = list1[s] 
    list2 = list2[s] 

    return len(set(list1)&set(list2))/float(len(set(list1) | set(list2))) 


if __name__ == "__main__": 

    list1 = [2,4,6,8,10] 
    list2 = [1,2,3,4,5] 

    a = [numpy.arange(.01,1.01,.01)] 
    for i in a: 
     print(score(list1, list2, i)) 

然而,當我嘗試運行這段代碼,我得到一個錯誤:

frac = int(frac) 
TypeError: only length-1 arrays can be converted to Python scalars 

這意味着,可變深度實際上是變量的列表(爲[0.01%,0.02 ..等等])。

我該如何解決這個問題,以便該函數一次只接受參數'深度'的一個參數,而不是整個列表中的內容?

感謝

+2

我對numpy一無所知,但如果你做了'a = numpy.arange(.01,1.01,.01)'?括號看起來多餘。 – Kevin

+0

感謝您的支持 – Labrat

回答

2

像凱文說,在評論,你的問題是要創建具有在你的代碼a = [numpy.arange(.01,1.01,.01)]它內部的另一個列表清單。只要刪除額外的括號,它會起作用。