list manipulation - Selecting sublists of different length if at least one element of the sublist fulfills a criterion
I have the following list, containing sublists with 1,2 or 3 elements.
list= {{10.7}, {10.5}, {9.83},{7.64}, {4.76}, {4.21,
5.64}, {3.75}, {3.4, 5.11}, {3.13, 4.76, 6.5},{7, 5, 3}}
I have to select sublists if at least one element of the sublist is between 5 and 8. I think it should work with Select[], but I can't figure it out, because the sublists have different lengths.
Answer
list = {{10.7}, {10.5}, {9.83}, {7.64}, {4.76}, {4.21, 5.64}, {3.75},
{3.4, 5.11}, {3.13, 4.76, 6.5}, {7, 5, 3}};
Select[list, Or @@ (5 <= # <= 8 & /@ #) &]
(* {{7.64}, {4.21, 5.64}, {3.4, 5.11}, {3.13, 4.76, 6.5}, {7, 5, 3}} *)
You get the same result using siblings of Select
:
Pick[list, Or @@ (5 <= # <= 8 & /@ #) & /@ list]
Cases[list, _?(Or @@ (5 <= # <= 8 & /@ #) &)]
Comments
Post a Comment