2015-04-05 76 views
1

我一直有這個代碼的一些問題,試圖結束與兩個一維數組的內部產品。感興趣的代碼如下所示:接收「ValueError:用序列設置數組元素」。

def find_percents(i): 
    percents=[] 
    median=1.5/(6+2*int(i/12)) 
    b=2*median 
    m=b/(7+2*int(i/12)) 
    for j in xrange (1,6+2*int(i/12)): 
     percents.append(float((b-m*j))) 
    percentlist=numpy.asarray(percents, dtype=float) 
    #print percentlist 
    total=sum(percentlist) 
    return total, percentlist 

def playerlister(i): 
    players=[] 
    for i in xrange(i+1,i+6+2*int(i/12)): 
     position=sheet.cell(i,2) 
     points=sheet.cell(i,24) 
     if re.findall('RB', str(position.value)): 
      vbd=points.value-rbs[24] 
      players.append(vbd) 
     else: 
      pass 
    playerlist=numpy.asarray(players, dtype=float) 
    return playerlist 

def others(i,percentlist,playerlist,total): 
    alternatives=[] 
    playerlist=playerlister(i) 
    percentlist=find_percents(i) 
    players=numpy.dot(playerlist,percentlist) 

我響應收到以下錯誤的這種附加代碼的最後一行:

ValueError: setting an array element with a sequence.

在此錯誤的大多數其他的例子,我有發現該錯誤是由於數組類型不正確percentlistplayerlist,但我的應該是浮動類型。如果有幫助的一切,我在以後的程序調用這些函數了一下,像這樣:

for i in xrange(1,30): 
    total, percentlist= find_percents(i) 
    playerlist= playerlister(i) 
    print type(playerlist[i]) 
    draft_score= others(i,percentlist,playerlist,total) 

誰能幫我弄清楚爲什麼我設置的數組元素與序列?請讓我知道,如果有更多的信息可能會有所幫助!同樣爲了清楚起見,playerlister正在利用xlrd模塊從電子表格中提取數據,但數據是數字並且測試顯示這兩個列表的類型爲numpy.float64

每個這些用於i一次迭代的形狀和內容是

<type 'numpy.float64'> 
(5,) 
[ 73.7 -94.4 140.9 44.8 130.9] 
(5,) 
[ 0.42857143 0.35714286 0.28571429 0.21428571 0.14285714] 

回答

1

你的功能find_percents返回一個兩元件的元組。 當您在others中調用它時,您將該元組綁定到名爲percentlist的變量,然後嘗試在點積中使用該變量。

我的猜測是,在others寫這它是固定的:

def others(i,percentlist,playerlist,total): 
    playerlist = playerlister(i) 
    _, percentlist = find_percents(i) 
    players = numpy.dot(playerlist,percentlist) 

前提當然playerlistpercentlist總是有相同數量的元素(我們不能因爲缺少電子表格的檢查) 。

要驗證,以下爲您重現它需要準確的錯誤信息和最少的代碼:

>>> import numpy as np 
>>> a = np.arange(5) 
>>> np.dot(a, (2, a)) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
ValueError: setting an array element with a sequence. 
相關問題