假设我有一个2x3 Ndaray:
[[0,1,1],
[1,1,1]]
我想用[0,0,0]
替换{第一个索引中有0的任何行}:
[[0,0,0],
[1,1,1]]
有没有可能用np实现这一点.哪里
import numpy as np
arr = np.array([[0,1,1],[1,1,1]])
replacement = np.full(arr.shape,[0,0,0])
new = np.where(arr[:,0]==0,replacement,arr)
我在最后一行遇到以下错误:
ValueError: operands could not be broadcast together with shapes (2,) (2,3) (2,3)
这个错误是有道理的,但我不知道如何修复代码来实现我的目标.任何建议都将不胜感激!
Edit:
如果我有这样的安排:
[[[0,1,1],[1,1,1],[1,1,1]],
[[1,1,1],[1,1,1],[1,1,1]],
[[1,1,1],[1,1,1],[1,1,1]]]
如何用[0,0,0]
替换第一个三元组?