5
$\begingroup$

Suppose I have a nested listed as follows,

{{{1,2},{3,4,5},{2,3}},{{2,3,4},{5,4,1},{1,4,3}},{{3,4,5},{3,4,5}}}

Where the elements are,

{{1,2},{3,4,5},{2,3}}

{{2,3,4},{5,4,1},{1,4,3}}

{{3,4,5},{3,4,5}}

I would I create a new list where I drop any sub element that has 1 or 2 in its first part. The output should then look like this,

{{{3,4,5}},{{5,4,1}},{{3,4,5},{3,4,5}}}
$\endgroup$
0

8 Answers 8

5
$\begingroup$
 lst = {{{1, 2}, {3, 4, 5}, {2, 3}}, {{2, 3, 4}, {5, 4, 1}, {1, 4,  3}}, 
        {{3, 4, 5}, {3, 4, 5}}};

 Select[#, FreeQ[#[[1]], 1 | 2] &] & /@ lst

or

 Pick[#, FreeQ[#[[1]], 1 | 2] & /@ #] & /@ lst

or

 Cases[#, _?(FreeQ[#[[1]], 1 | 2] &)] & /@ lst

or

 DeleteCases[#, _?(! FreeQ[#[[1]], 1 | 2] &)] & /@ lst

all give

{{3, 4, 5}}, {{5, 4, 1}}, {{3, 4, 5}, {3, 4, 5}}} 
$\endgroup$
2
  • $\begingroup$ @Ibrahim, you might want to wait a while before accepting an answer to encourage alternative answers. Welcome to mma.se. $\endgroup$
    – kglr
    Commented Jul 28, 2014 at 20:59
  • $\begingroup$ @RunnyKine, thank you.. "almost" is critical :) $\endgroup$
    – kglr
    Commented Jul 28, 2014 at 21:11
3
$\begingroup$
list = {{{1, 2}, {3, 4, 5}, {2, 3}}, {{2, 3, 4}, {5, 4, 1}, {1, 4, 3}}, {{3, 4, 5}, {3, 4, 5}}}

Then:

Cases[{Except[1 | 2], __}] /@ list  (* v10 syntax *)

{{{3, 4, 5}}, {{5, 4, 1}}, {{3, 4, 5}, {3, 4, 5}}}

OR

Map[Cases[{Except[1 | 2], __}]]@list  (* v10 operator form *)

OR

Select[#, #[[1]] != 1 && #[[1]] != 2 &] & /@ list

OR

Select[#[[1]] != 1 && #[[1]] != 2 &] /@ list  (* v10 syntax *)
$\endgroup$
2
  • $\begingroup$ Indeed, a subtle but significant change between V9 and V10. How did you discover it? Is it in the documentation? $\endgroup$
    – eldo
    Commented Jul 28, 2014 at 21:53
  • $\begingroup$ @eldo. Yes it's in the documentation, I believe that's where I learned it. $\endgroup$
    – RunnyKine
    Commented Jul 28, 2014 at 22:01
2
$\begingroup$

DeleteCases with levelspec:

expr = {{{1, 2}, {3, 4, 5}, {2, 3}}, {{2, 3, 4}, {5, 4, 1}, {1, 4, 3}}, {{3, 4, 5}, {3, 4, 5}}};

DeleteCases[expr, {1 | 2, __}, {2}]
{{{3, 4, 5}}, {{5, 4, 1}}, {{3, 4, 5}, {3, 4, 5}}}

Or, inspired by RunnyKine's answer, in v10 operator syntax:

DeleteCases[{1 | 2, __}] /@ expr
{{{3, 4, 5}}, {{5, 4, 1}}, {{3, 4, 5}, {3, 4, 5}}}
$\endgroup$
5
  • $\begingroup$ +1 I tried using levelspec but kept botching it $\endgroup$
    – RunnyKine
    Commented Jul 28, 2014 at 22:43
  • $\begingroup$ @RunnyKine I like your use of the operator syntax, by the way. :-) $\endgroup$
    – Mr.Wizard
    Commented Jul 28, 2014 at 22:43
  • $\begingroup$ Thanks, I'm really liking the new operator forms. :) $\endgroup$
    – RunnyKine
    Commented Jul 28, 2014 at 22:45
  • $\begingroup$ @RunnyKine I just use Infinity because I'm lazy. $\endgroup$ Commented Jul 28, 2014 at 23:05
  • $\begingroup$ @seismatica That's fine, but be aware that it can come at a significant cost to performance. Most of the time not, but sometimes. Starting perhaps a year and a half ago I try to be more specific with my use of levelspec. $\endgroup$
    – Mr.Wizard
    Commented Jul 29, 2014 at 0:39
1
$\begingroup$

Since all the good answers have been given, here's a dumb one:

expr = {{{1, 2}, {3, 4, 5}, {2, 3}}, {{2, 3, 4}, {5, 4, 1}, {1, 4, 3}}, {{3, 4, 5}, {3, 4, 5}}};
expr //. {pre___, {1 | 2, ___}, post___} :> {pre, post}
$\endgroup$
1
$\begingroup$

If the list is all numbers, here's another way:

list = {{{1, 2}, {3, 4, 5}, {2, 3}}, {{2, 3, 4}, {5, 4, 1}, {1, 4, 3}}, {{3, 4, 5}, {3, 4, 5}}};
drop = {1, 2};

Pick[list, 
 Unitize[
    Evaluate[Times @@ (# - drop)] & @
     Map[
      First,
      list,
      {2}]
  ],
 1
 ]
(*
  {{{3, 4, 5}}, {{5, 4, 1}}, {{3, 4, 5}, {3, 4, 5}}}
*)

Edit: changed the function applied to Map (thanks, Mr.Wizard), which originally was

Evaluate[ReleaseHold @ Fold[#1 (Hold[Slot][1] - #2) &, 1, drop]] &
$\endgroup$
5
  • $\begingroup$ That's a twisted use of Hold[Slot], sure to confuse half the people who read this, and really quite unnecessary as well. Naturally, +1. :D $\endgroup$
    – Mr.Wizard
    Commented Jul 29, 2014 at 0:47
  • $\begingroup$ @Mr.Wizard I guess so, or I'm getting tired. BTW, I tried Inactive but it was slower than Hold, I think just by a constant (not depending on list). Not sure why. Maybe I'll fix the Hold... :) $\endgroup$
    – Michael E2
    Commented Jul 29, 2014 at 0:50
  • $\begingroup$ Really I figured it was intentional obfuscation. Can't you just write Evaluate[Times @@ (# - drop)] &? Or maybe I'm tired. $\endgroup$
    – Mr.Wizard
    Commented Jul 29, 2014 at 0:56
  • $\begingroup$ @Mr.Wizard No, I think it's me who's tired. I started with something else that morphed while my brain wore out. Thanks for the simple code. $\endgroup$
    – Michael E2
    Commented Jul 29, 2014 at 0:59
  • $\begingroup$ Ah, one of those sessions. Say no more. :o) $\endgroup$
    – Mr.Wizard
    Commented Jul 29, 2014 at 0:59
1
$\begingroup$
list = {{{1, 2}, {3, 4, 5}, {2, 3}},
        {{2, 3, 4}, {5, 4, 1}, {1, 4, 3}},
        {{3, 4, 5}, {3, 4, 5}}};

Using Replace at level 2:

Replace[list, {1 | 2, __} :> Nothing, {2}]

(*{{{3, 4, 5}}, {{5, 4, 1}}, {{3, 4, 5}, {3, 4, 5}}}*)

Or using Position and Delete:

Delete[#, Position[#, x_List /; ! FreeQ[x[[1]], 1 | 2], {2}]] &@list

(*{{{3, 4, 5}}, {{5, 4, 1}}, {{3, 4, 5}, {3, 4, 5}}}*)
$\endgroup$
1
$\begingroup$
list =
 {{{1, 2}, {3, 4, 5}, {2, 3}}, 
  {{2, 3, 4}, {5, 4, 1}, {1, 4, 3}}, 
  {{3, 4, 5}, {3, 4, 5}}};

Using ReplaceAt (new in 13.1)

ReplaceAt[list, {1 | 2, __} :> Nothing, {;; , ;;}]

{{{3, 4, 5}}, {{5, 4, 1}}, {{3, 4, 5}, {3, 4, 5}}}

Using function deconstruction

f[{1 | 2, __}] := Nothing

f[a_] := a

Map[f, list, {2}]

{{{3, 4, 5}}, {{5, 4, 1}}, {{3, 4, 5}, {3, 4, 5}}}

$\endgroup$
1
$\begingroup$
list = {{{1, 2}, {3, 4, 5}, {2, 3}}, {{2, 3, 4}, {5, 4, 1}, {1, 4, 
     3}}, {{3, 4, 5}, {3, 4, 5}}};

f = First@# =!= 1 && First@# =!= 2 &;

Select[#, f] & /@ list
DeleteCases[list, _?(Not@*f), {2}]
Map[f, list, {2}] // Pick[list, #] &

{{{3, 4, 5}}, {{5, 4, 1}}, {{3, 4, 5}, {3, 4, 5}}}

$\endgroup$

Your Answer

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

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