Manifesto Mr Asked:2020-05-26 16:07:19 +0000 UTC2020-05-26 16:07:19 +0000 UTC 2020-05-26 16:07:19 +0000 UTC 如何按条件替换数组中的所有值? 772 是否可以将数组中的某些值替换为其他值,例如将4全部替换为0? python 2 个回答 Voted Best Answer MaxU - stop genocide of UA 2020-05-26T16:08:38Z2020-05-26T16:08:38Z 试试这样: 例子: b = a.copy() b[b==4] = 0 MarianD 2020-05-28T20:46:26Z2020-05-28T20:46:26Z np.where(a == 4, 0, a) 测试: In[1]: a = np.random.randint(1, 5, 20) In[2]: a Out[2]: array([4, 1, 4, 2, 4, 3, 1, 2, 1, 2, 4, 1, 4, 2, 4, 3, 1, 1, 1, 3]) In[3]: np.where(a == 4, 0, a) Out[3]: array([0, 1, 0, 2, 0, 3, 1, 2, 1, 2, 0, 1, 0, 2, 0, 3, 1, 1, 1, 3])
试试这样:
例子:
测试: