0

I have an array x and a list of filters (array of booleans same length as x):

x = np.random.rand(10)
filt1 = x > .2
filt2 = x < .5
filt3 = x % 2 > .02
filters_list = [filt1, filt2, filt3]

I want to create a filter that is the logical AND of all filters in filters_list so the output should be

output = x[filt1 & filt2 & filt3]

How do I create the filter filt1 & filt2 & filt3 from filters_list assuming len(filters_list) is arbitrary?

user3483203
  • 50,081
  • 9
  • 65
  • 94
Nic
  • 637
  • 2
  • 6
  • 19

1 Answers1

0

You can use numpy.all() with the axis and the list of filters.

x = np.arange(10)
filt1 = x > 2
filt2 = x < 9
filt3 = (x % 2) == 1
filters_list = np.all([filt1, filt2, filt3], axis=0)
x[filters_list]

#array([3, 5, 7])
Mark
  • 90,562
  • 7
  • 108
  • 148