Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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}}}
share|improve this question

5 Answers 5

up vote 3 down vote accepted
 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}}} 
share|improve this answer
    
Thank you. What I was looking for. –  Ibrahim Jul 28 at 20:43
    
@Ibrahim, you might want to wait a while before accepting an answer to encourage alternative answers. Welcome to mma.se. –  kguler Jul 28 at 20:59
    
+1. You gave almost all the methods :). –  RunnyKine Jul 28 at 21:08
    
@RunnyKine, thank you.. "almost" is critical :) –  kguler Jul 28 at 21:11
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 *)
share|improve this answer
    
Indeed, a subtle but significant change between V9 and V10. How did you discover it? Is it in the documentation? –  eldo Jul 28 at 21:53
    
@eldo. Yes it's in the documentation, I believe that's where I learned it. –  RunnyKine Jul 28 at 22:01

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}}}
share|improve this answer
    
+1 I tried using levelspec but kept botching it –  RunnyKine Jul 28 at 22:43
    
@RunnyKine I like your use of the operator syntax, by the way. :-) –  Mr.Wizard Jul 28 at 22:43
    
Thanks, I'm really liking the new operator forms. :) –  RunnyKine Jul 28 at 22:45
    
@RunnyKine I just use Infinity because I'm lazy. –  seismatica Jul 28 at 23:05
    
@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. –  Mr.Wizard Jul 29 at 0:39

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]] &
share|improve this answer
    
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 –  Mr.Wizard Jul 29 at 0:47
    
@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... :) –  Michael E2 Jul 29 at 0:50
    
Really I figured it was intentional obfuscation. Can't you just write Evaluate[Times @@ (# - drop)] &? Or maybe I'm tired. –  Mr.Wizard Jul 29 at 0:56
    
@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. –  Michael E2 Jul 29 at 0:59
    
Ah, one of those sessions. Say no more. :o) –  Mr.Wizard Jul 29 at 0:59

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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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