How can I generate a 2D boolean array using a list of tuples that shows the indices of the True values?
For example I have the following list of tuples:
lst = [(0,1), (0, 2), (1, 0), (1, 3), (2,1)]
What I do is, I first generate an array of False's:
arr = np.repeat(False, 12).reshape(3, 4)
Then, iterate over the list to assign True values:
for tup in lst:
arr[tup] = True
print(arr)
array([[False, True, True, False],
[ True, False, False, True],
[False, True, False, False]], dtype=bool)
It seems like a common use case to me so I was wondering if there is a built-in method for this, without the loops.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…