IMO OP will eigentlich nicht np.bitwise_and()
(aka &
), sondern eigentlich, np.logical_and()
weil sie logische Werte wie True
und vergleichen False
- siehe diesen SO-Beitrag auf logisch vs. bitweise , um den Unterschied zu sehen.
>>> x = array([5, 2, 3, 1, 4, 5])
>>> y = array(['f','o','o','b','a','r'])
>>> output = y[np.logical_and(x > 1, x < 5)] # desired output is ['o','o','a']
>>> output
array(['o', 'o', 'a'],
dtype='|S1')
Ein gleichwertiger Weg, dies zu tun, besteht darin np.all()
, das axis
Argument entsprechend festzulegen.
>>> output = y[np.all([x > 1, x < 5], axis=0)] # desired output is ['o','o','a']
>>> output
array(['o', 'o', 'a'],
dtype='|S1')
durch die Zahlen:
>>> %timeit (a < b) & (b < c)
The slowest run took 32.97 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 1.15 µs per loop
>>> %timeit np.logical_and(a < b, b < c)
The slowest run took 32.59 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 1.17 µs per loop
>>> %timeit np.all([a < b, b < c], 0)
The slowest run took 67.47 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 5.06 µs per loop
so mit np.all()
langsamer, aber &
und logical_and
sind in etwa gleich.