Python脚本:
import numpy as np a = np.random.rand(8, 8) b = np.full_like(a, -1) n = 0 for i, val in np.ndenumerate(a): if val < 0.666: b[i] = n n += 1
字符串创建数组b,在a < 0.666(任意条件的占位符)和-1的位置使用连续的自然数。是否有魔术NumPy表达式产生相同的结果?
b
a < 0.666
-1
noj0wjuj1#
import numpy as np a = np.random.rand(8, 8) b = np.full_like(a, -1) mask = a<0.666 b[mask] = np.arange(mask.sum()) print(a) print(b)
字符串
1条答案
按热度按时间noj0wjuj1#
字符串