【发布时间】:2017-04-01 04:18:15
【问题描述】:
我想将值附加到选定的数组中,而不必经过 for 循环。
即如果我想将 0 值添加到数组的某些位置:
a=np.array([[1,2,3,4,5],[1,2,3,4,5]])
condition=np.where(a>2)
a[condition]=np.append(a[condition],np.array([0]*len(condition[0])))
-> ValueError: shape mismatch: value array of shape (12,) could not be broadcast to indexing result of shape (6,)
编辑澄清:
我需要向选定的数组位置添加值(和维度,如果需要)。循环看起来像这样:
for t in range(len(ind)):
c = cols[t]
r = rows[t]
if data1[r, c] > 2:
data2[r,c]=np.append(data2[r,c],t)
有什么办法可以消除这个循环(~100 000 次迭代)?谢谢
【问题讨论】:
-
想要的结果是什么?
标签: python arrays numpy append