Python 3.x 在python中访问结构中的位置
我想使用一个包含8列和df1的数组N。塑造[0]行,我想比较数组状态和另一个数组动作的值,并根据状态i的值和动作j的值在适当位置[i,j]增加N的值,如下所示,但我得到了这个错误,任何建议:Python 3.x 在python中访问结构中的位置,python-3.x,Python 3.x,我想使用一个包含8列和df1的数组N。塑造[0]行,我想比较数组状态和另一个数组动作的值,并根据状态i的值和动作j的值在适当位置[i,j]增加N的值,如下所示,但我得到了这个错误,任何建议: ValueError Traceback (most recent call last) <ipython-input-79-66d1d4ab2df8> in <module>() 84 elif(sta
ValueError Traceback (most recent call last)
<ipython-input-79-66d1d4ab2df8> in <module>()
84 elif(state[i]==4 and action[i]==0):
85 #access column 6 of N represents state1 for action 0
---> 86 N[i:6]= N[i-1:6]+1
87 else:
88 #access column 7 of N represents state1 for action 1
ValueError: could not broadcast input array from shape (2,8) into shape (1,8)
你能详细解释一下“它不起作用”是什么意思吗?到底是什么不起作用?它做了什么?谢谢你的评论我已经把我的error@glibdud我想我不是把N[i,j]放进去,而是把N[i:j]放进去,我想增强这段代码,任何建议都可以
N = np.zeros((df1.shape[0], 8))
#for i in range(1,df1.shape[0],1):
#N[df1['state'][i],df1['action'][i]]=N[state[i-1],action[i-1]]+1
for i in range(1,df1.shape[0],1):
if(state[i]==1 and action[i]==0):
#access column 0 of N represents state1 for action 0
N[i,0] = N[i-1,0]+1
elif(state[i]==1 and action[i]==1):
#access column 1 of N represents state1 for action 1
N[i,1] = N[i-1,1]+1
elif(state[i]==2 and action[i]==0):
#access column 2 of N represents state1 for action 0
N[i,2] = N[i-1,2]+1
elif(state[i]==2 and action[i]==1):
#access column 3 of N represents state1 for action 1
N[i,3] = N[i-1,3]+1
elif(state[i]==3 and action[i]==0):
#access column 4 of N represents state1 for action 0
N[i,4] = N[i-1,4]+1
elif(state[i]==3 and action[i]==1):
#access column 5 of N represents state1 for action 1
N[i,5]= N[i-1,5]+1
elif(state[i]==4 and action[i]==0):
#access column 6 of N represents state1 for action 0
N[i,6]= N[i-1,6]+1
else:
#access column 7 of N represents state1 for action 1
N[i,7]= N[i-1,7]+1