# Selecting specific elements of a list satisfying some criteria

I have a list of 1000 sublists and each sublist is length 400. I'd like to produce a new set containing only those sublists where element 25, 75 and 250 satisfy some criteria. The following works on sublists of length 3 with some toy criteria:

Cases[set, {a_, b_, c_} /; 1 < a < 5 && 0 < b < 4 && 4 < c < 8], where set is the list of 1000 sublists.

How may I construct a list similar to {a_,b_,c_} but that is instead 400 elements? That is I want to produce a list like {a1_,a2_,a3_,....,a400_}. Probably it's simple but I've been playing about with array and Append etc. and not found a way.

• Better use Select with the selecting funtion that directly accesses the values of the sublist. – Henrik Schumacher Apr 28 '20 at 14:44
• @HenrikSchumcher Yes, but if possible I would like to create this list of 400 elements in any case. – CAF Apr 28 '20 at 14:45

Yes, but if possible I would like to create this list of 400 elements in any case.

No, you quite likely do not want to construct this list. You should rather use something like the following:

set = RandomReal[{-10, 10}, {1000, 400}];
result1 = Select[
set,
1 < #[[25]] < 5 && 0 < #[[75]] < 4 && 4 < #[[250]] < 8 &
]; // RepeatedTiming // First


0.0019

Alternatively, one can do something like

result2 = Pick[
set,
1 < #25 < 5 && 0 < #75 < 4 && 4 < #250 < 8 & @@@ set
]; // RepeatedTiming // First


0.032

However, I do not recommend it, because, as you can see, it is super slow. However, Pick is really fast, if used with vectorized code:

result3 = Pick[
set,
Times[
UnitStep[# - 1.] UnitStep[Subtract[5., #]] &[set[[All, 25]]],
UnitStep[# - 0.] UnitStep[Subtract[4., #]] &[set[[All, 75]]],
UnitStep[# - 4.] UnitStep[Subtract[8., #]] &[set[[All, 250]]]
],
1
]; // RepeatedTiming // First


0.0000686

Of course, all results are equal:

result1 == result2 == result3


True

• Thank you very much. This works for me. Just out of interest, is it possible to create such a list? Say if one needed to define a function of an arbitrary number of variables, f[a_,b_,c_,...] etc.. then construction of such a list might be useful. – CAF Apr 28 '20 at 14:53
• Well, "pure" functions constructed with & have actually an arbitrary number of arguments... – Henrik Schumacher Apr 28 '20 at 15:10