3
$\begingroup$

I have a list such as this:

    {{{-1, 0, 0, 1, 1, _, _, _, _}, {-1, 0, -1, 2,  1, _, _, _, _}, {4}, {-1, 1, 0, 1, 1, _, _, _, _}}, {{0, -1, 0, 1, 1, _, _, _, _}, {0, -1, -1, 2, 1, _, _, _, _}, {1, -1, 0, 1,  1, _, _, _, _}, {4}}, {{4}, {0, 0, -1, 2, 1, _, _, _, _}, {1, 0, 0, 1, 1, _, _, _, _}, {0, 1, 0, 1, 1, _, _, _, _}}, {{1}, {2}, {3}, {0, 0, 0, 1, 2}}, {{4}, {0, 0, 0, 2, 1, _, _, _, _}, {-1, 0, 0, 2, 1, _, _, _, _}, {0, -1, 0, 2, 1, _, _, _, _}}}

I want to remove the elements that have length=9 such as {-1, 0, 0, 1, 1, _, _, _, _} leaving the elements that have length=1 such as {4}. I have tried many approaches using delete cases but with no success.

$\endgroup$
  • 3
    $\begingroup$ newList=Select[listOfLists,Length[#]!=9&] $\endgroup$ – David Keith Mar 7 '17 at 22:15
  • $\begingroup$ Just to clarify: what is your desired outcome? Cases[list, Except[ConstantArray[_, 9]], {2}] giving {{4}, {4}, {4}, {1}, {2}, {3}, {0, 0, 0, 1, 2}, {4}} or list /. ConstantArray[_, 9] -> Nothing giving {{{4}}, {{4}}, {{4}}, {{1}, {2}, {3}, {0, 0, 0, 1, 2}}, {{4}}} ? $\endgroup$ – user1066 Mar 7 '17 at 22:40
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Michael E2 Mar 7 '17 at 23:55
3
$\begingroup$
DeleteCases[#, x_ /; Length[x] == 9] & /@ yourList
| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ Might be more natural with a Select statement: Select[#,GreaterThan[9]@*Length]. (Note that this uses GreaterThan which was introduced in 10.3). $\endgroup$ – b3m2a1 Mar 8 '17 at 3:25
3
$\begingroup$

Another way:

Pick[list, Unitize[Map[Length, list, {2}] - 9], 1]

If speed is an issue, Pick is around twice as fast on large lists as DeleteCases or Select:

data2 = Join @@ Table[data, {1000}];
Pick[data2, Unitize[Map[Length, data2, {2}] - 9], 1] // Flatten // Length // RepeatedTiming
DeleteCases[#, x_ /; Length[x] == 9] & /@ data2 // Flatten // Length // RepeatedTiming
Select[#, LessThan[9]@*Length] & /@ data2 // Flatten // Length // RepeatedTiming
Select[#, Length[#] != 9 &] & /@ data2 // Flatten // Length // RepeatedTiming
(*
  {0.010, 12000}  Pick
  {0.020, 12000}  DeleteCases
  {0.021, 12000}  Select, assuming no sublists longer than 9
  {0.018, 12000}  Select, general case
*)
| improve this answer | |
$\endgroup$
1
$\begingroup$

Doing it using recursion:

deleteRecursively[{}] = {};
deleteRecursively[{x_Integer}] := {x};
deleteRecursively[x : {_List ..}] := With[{crit = 9},
  If[Length@First[x] == crit, Join[{Null}, deleteRecursively[Rest@x]],
  Join[{First@x}, deleteRecursively[Rest@x]]
 ]
]

result = Map[deleteRecursively]@list

(* {{Null, Null, {4}, Null}, {Null, Null, Null, {4}}, {{4}, Null, Null, Null},
{{1}, {2}, {3}, {0, 0, 0, 1, 2}}, {{4}, Null, Null, Null}} *) 

You can remove Null (which i have used to preserve the structure of your list) using DeleteCases[result, Null, {2}]

(* {{{4}}, {{4}}, {{4}}, {{1}, {2}, {3}, {0, 0, 0, 1, 2}}, {{4}}} *)
| improve this answer | |
$\endgroup$
1
$\begingroup$

Here is a slot-free version using the newish operator forms (I like slot-free operator forms):

Select[UnequalTo[9] @* Length] /@ list

{{{4}}, {{4}}, {{4}}, {{1}, {2}, {3}, {0, 0, 0, 1, 2}}, {{4}}}

| improve this answer | |
$\endgroup$
  • $\begingroup$ cool use of UnequalTo ! $\endgroup$ – Ali Hashmi Dec 14 '17 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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