Python “接收”;ValueError:使用序列设置数组元素。";
我在这段代码中遇到了一些问题,试图得到两个一维数组的内积。感兴趣的代码如下所示:Python “接收”;ValueError:使用序列设置数组元素。";,python,arrays,numpy,xlrd,Python,Arrays,Numpy,Xlrd,我在这段代码中遇到了一些问题,试图得到两个一维数组的内积。感兴趣的代码如下所示: 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.
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:使用序列设置数组元素
在这个错误的大多数其他示例中,我发现错误是由于数组百分比列表
和playerlist
中的数据类型不正确造成的,但我的应该是float类型。如果有帮助的话,我稍后在程序中调用这些函数,如下所示:
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]
(5,)
[ 73.7 -94.4 140.9 44.8 130.9]
(5,)
[ 0.42857143 0.35714286 0.28571429 0.21428571 0.14285714]
您的函数find_percents
返回一个两元素元组。
当您在others
中调用它时,您将该元组绑定到名为percentlist
的变量,然后尝试在点积中使用该变量
我的猜测是,通过在others
中写入此内容,它是固定的:
def others(i,percentlist,playerlist,total):
playerlist = playerlister(i)
_, percentlist = find_percents(i)
players = numpy.dot(playerlist,percentlist)
当然,playerlist
和percentlist
始终具有相同数量的元素(由于缺少电子表格,我们无法检查)
为了验证,以下内容为您提供了准确的错误消息以及再现该消息所需的最少代码:
>>> 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.
>>将numpy作为np导入
>>>a=np.arange(5)
>>>np.dot(a,(2,a))
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
ValueError:使用序列设置数组元素。