python - Pythonista way of extracting elements from an array -
i'm writing few python lines of code doing following:
i have 2 arrays , b, b contains (non strictly) increasing integers.
i want extract values values of b multiple of 20 don't want duplicates, in sense if b has values : ...,40,40,41,... want first value in corresponding 40 not sec one.
that's why a[b%20==0]
not work.
i've been using:
factors = [20*i in xrange(1,int(b[-1]/20 +1))] sample = numpy.array([a[numpy.nonzero(b==factor)[0][0]] factor in factors])
but both slow , inelegant.
is there pythonista 'cute' way of doing it?
a[(b % 20 == 0) & np.r_[true, np.diff(b) > 0]]
the b % 20 == 0
part gives binary mask selects elements of b factor of 20. np.r_[true, np.diff(b) > 0]
part creates binary mask selects elements differ previous element (we explicitly add together true @ beginning, first element not have previous element). add together masks , voila!
python numpy
No comments:
Post a Comment