对于下面的数组,np.extract产生0,0结果,而对于其他数组,它给出满足条件的行数。因此,对于这一次,我预计结果为3,6。
你能帮我找出哪里出故障了吗?
print(type(perm_list))
<class 'numpy.ndarray'>
print(perm_list.shape)
(8, 5)
print(perm_list)
[[0 0 0 0 0]
[0 0 0 0 0]
[4 7 0 0 0]
[2 6 2 0 0]
[0 0 0 0 0]
[0 0 0 0 0]
[2 6 2 0 0]
[4 7 0 0 0]]
perm_list_mask = np.extract(perm_list[:, 2] == 2, perm_list)
print(perm_list_mask)
[0 0]下面是我在脚本中使用的另一个数组,它可以无缝地工作:
a = np.array([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [8, 10, 4, 3], [8, 1, 5, 5]])
b = np.extract(a[:, 0] == 8, a)
print(b)
[2 3 4]发布于 2020-04-26 19:39:59
您得到0,0作为答案的原因是因为np.extract要求条件中的数组与传入第二个参数的数组的形状相同。perm_list:,2与perm_list的形状不同。下面更正了这一点,并返回2,2,正如mkieger1所说的那样。下面是返回你正在寻找的答案的代码。
def p():
perm_list = np.array([[0,0,0,0,0],[0,0,0,0,0],[4,7,0,0,0],[2,6,2,0,0],[0,0,0,0,0],[0,0,0,0,0],[2,6,2,0,0],[4,7,0,0,0]])
perm_list_mask = np.extract(perm_list[:, 2] == 2, perm_list[:,2])
print(perm_list_mask)
def pSolved():
perm_list = np.array([[0,0,0,0,0],[0,0,0,0,0],[4,7,0,0,0],[2,6,2,0,0],[0,0,0,0,0],[0,0,0,0,0],[2,6,2,0,0],[4,7,0,0,0]])
return np.where(perm_list[:, 2] == 2)输出:
>>> p()
[2 2]
>>> pSolved()
(array([3, 6]),)https://stackoverflow.com/questions/61439730
复制相似问题