根据列表替换数组中的值的简单示例:
import numpy as np
l = [1,3,4,15]
a = np.array([1,1,2,4,6,7,8,9,1,2,3,4,89,12,23,3,4,10,15])
for element in l:
a = np.where(a == element, 0, a)
由于这相当慢,我正在寻找一种更快、可扩展的替代方案。
最佳答案
np.where(np.in1d(a, l), 0, a)
array([ 0, 0, 2, 0, 6, 7, 8, 9, 0, 2, 0, 0, 89, 12, 23, 0, 0,
10, 0])
关于python - 替换 numpy 数组中多个元素的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60110135/