如何拒绝项目清单?

How to make denial of the list of items?

我有包含一些值的列表,我想从我的列表中排除 而不是 值并将它们替换为 0。

即伪代码:

for i in range(0,img.shape[0]):
for j in range(0,img.shape[1]):
    if img[i][j]!=[some values]:
        img[i][j]=0

这是一种方法:

In [1]: l = [1, 2, 1, 10, 2, 1, 3, 10]

In [2]: exclusions = [1, 2]

In [3]: [el if el not in exclusions else 0 for el in l]
Out[3]: [0, 0, 0, 10, 0, 0, 3, 10]

根据 exclusions 的大小,如果将其转换为 set,代码的性能可能会更高。