Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
m1 = [0,1,1,3] m2 = [0,0,1,1] data = [10,20,30,40]
我想做这样的事情:
mask = (m1 == 1) & (m2 == 1) data[mask] #should return 30
请注意,此示例会导致错误
您正在使用 python 列表而不是 numpy 数组。试试这个:
import numpy as np m1 = np.array([0,1,1,3]) m2 = np.array([0,0,1,1]) mask = (m1 == 1) & (m2 == 1) data[mask] # returns array([30])
在您的示例中,当m1是一个列表时,m1 == 1被评估为False(对于 相同m2),所以掩码是False和data[False] = data[0] = 10。
m1
m1 == 1
False
m2
data[False] = data[0] = 10