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.
假设我有一个数字数组:
a1 = np.arange(1,(30)+1)[:,None] # or some other way og making (n,1) array
我想把每 4 个 nr 拿出来并命名为别的,我会这样做:
a2 = aaa[0:30:2]
那很好,但是我如何取出除上述数字之外的所有数字?换句话说:
[[2.] [3.] [5.] [6.] [8.] [9.]....
我不知道该怎么做!
创建一个布尔掩码并将其应用于您的数组:
>>> a2 = np.ones_like(a1) >>> a2[::3] = 0 >>> a1[a2.astype(bool)] array([ 2, 3, 5, 6, 8, 9, 11, 12, 14, 15, 17, 18, 20, 21, 23, 24, 26, 27, 29, 30])
如果要提取其他元素,只需应用反向布尔掩码:
>>> a1[~a2.astype(bool)] array([ 1, 4, 7, 10, 13, 16, 19, 22, 25, 28])