Name

compress

Synopsis

compress(condition,a,axis=0)

Returns an array c with the same type code and rank as a. c includes only the elements of a for which the item of condition, corresponding along the given axis, is true. For example, compress((1,0,1),a ) = = take( a ,(0,2),0) since (1,0,1) has true values only at indices 0 and 2. Here’s how to get only the even numbers from an array:

a = Numeric.arange(10)
print Numeric.compress(a%2=  =0, a)   # prints: [0 2 4 6 8]

Get Python in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.