5
$\begingroup$

I would like to parse every sublist of a nested list and select only those elements that have a certain length, i.e., smaller than 10 elements while maintaining the list structure. Is there any quick trick that makes it work ?

Matrix = {
 {
  {
   {O1,2,22,24,28,14,D1},
   {O1,1,21,23,27,13,D1},
   {O1,2,22,24,28,27,13,D1},
   {O1,2,22,24,28,14,13,D1},
   {O1,2,22,24,23,27,13,D1},
   {O1,2,22,21,23,27,13,D1},
   {O1,2,1,21,23,27,13,D1},
   {O1,1,21,23,27,28,14,D1},
   {O1,1,21,23,27,13,14,D1},
   {O1,1,21,23,24,28,14,D1},
   {O1,1,21,22,24,28,14,D1},
   {O1,1,2,22,24,28,14,D1},
   {O1,2,22,24,28,27,13,14,D1},
   {O1,2,22,24,23,27,28,14,D1},
   {O1,1,2,22,21,23,25,9,10,26,24,28,18,17,27,13,14,D1},
   {O1,1,2,22,21,23,25,9,10,26,24,28,16,15,27,13,14,D1},
   {O1,1,2,22,21,23,25,7,8,26,24,28,20,19,27,13,14,D1},
   {O1,1,2,22,21,23,25,7,8,26,24,28,18,PoP2,17,27,13,D1},
   {O1,1,2,22,21,23,25,7,8,26,24,28,18,17,27,13,14,D1},
   {O1,1,2,22,21,23,25,7,8,26,24,28,16,15,27,13,14,D1},
   {O1,1,2,22,21,23,5,PoP1,6,24,28,18,PoP2,17,27,13,14,D1},
   {O1,2,1,21,22,24,26,12,11,25,23,27,17,PoP2,18,28,14,13,D1},
   {O1,2,1,21,22,24,26,10,9,25,23,27,17,PoP2,18,28,14,13,D1},
   {O1,2,1,21,22,24,26,8,7,25,23,27,17,PoP2,18,28,14,13,D1},
   {O1,1,2,22,21,23,25,11,12,26,24,28,18,PoP2,17,27,13,14,D1},
   {O1,1,2,22,21,23,25,9,10,26,24,28,18,PoP2,17,27,13,14,D1},
   {O1,1,2,22,21,23,25,7,8,26,24,28,18,PoP2,17,27,13,14,D1}
  }
 }
}

the result needs to be :

{
 {
  {
   {O1,2,22,24,28,14,D1},
   {O1,1,21,23,27,13,D1},
   {O1,2,22,24,28,27,13,D1},
   {O1,2,22,24,28,14,13,D1},
   {O1,2,22,24,23,27,13,D1},
   {O1,2,22,21,23,27,13,D1},
   {O1,2,1,21,23,27,13,D1},
   {O1,1,21,23,27,28,14,D1},
   {O1,1,21,23,27,13,14,D1},
   {O1,1,21,23,24,28,14,D1},
   {O1,1,21,22,24,28,14,D1},
   {O1,1,2,22,24,28,14,D1},
   {O1,2,22,24,28,27,13,14,D1},
   {O1,2,22,24,23,27,28,14,D1}
  }
 }
}

Appreciated any help,

$\endgroup$

2 Answers 2

6
$\begingroup$
Map[Select[Length[#] < 10 &], Matrix, {2}]
$\endgroup$
2
$\begingroup$

I would again use DeleteCases just like in my answer to your last question Filtering nested lists while maintaining the list level:

DeleteCases[Matrix, s_List?(Length[#]>=10&), {-2}]

{{{{O1, 2, 22, 24, 28, 14, D1}, {O1, 1, 21, 23, 27, 13, D1}, {O1, 2, 22, 24, 28, 27, 13, D1}, {O1, 2, 22, 24, 28, 14, 13, D1}, {O1, 2, 22, 24, 23, 27, 13, D1}, {O1, 2, 22, 21, 23, 27, 13, D1}, {O1, 2, 1, 21, 23, 27, 13, D1}, {O1, 1, 21, 23, 27, 28, 14, D1}, {O1, 1, 21, 23, 27, 13, 14, D1}, {O1, 1, 21, 23, 24, 28, 14, D1}, {O1, 1, 21, 22, 24, 28, 14, D1}, {O1, 1, 2, 22, 24, 28, 14, D1}, {O1, 2, 22, 24, 28, 27, 13, 14, D1}, {O1, 2, 22, 24, 23, 27, 28, 14, D1}}}}

$\endgroup$
1
  • $\begingroup$ Not sure why, but applying this does not work correctly. Whereas, the answer given by Mr. Schumacher works. Thanks anyway. $\endgroup$
    – Besi
    Nov 1, 2018 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.