Re: Filtering a list of list for certain elements that are neighbours

• To: mathgroup at smc.vnet.net
• Subject: [mg88895] Re: Filtering a list of list for certain elements that are neighbours
• From: Steven Siew <stevensiew2 at gmail.com>
• Date: Tue, 20 May 2008 06:53:51 -0400 (EDT)
• References: <g0p4a2\$nc4\$1@smc.vnet.net>

Thank you everyone who responded to me. I was busy for a few days but
now I can respond.

I have listed all those who has responded below.

A big big thank you to all of you who has contributed. I was amazed at
the different ways of doing exactly the same thing.

Steven Siew

-------------------

I have a list of lists

mylist={{a,b,c},{a,c,b},{b,a,c},{b,c,a},{c,a,b},{c,b,a}}

I want to filter out all lists that have "a being a neighbor to b".

I am looking for the following result.

{{a,c,b},{b,c,a}}

-------------------

(1) W Craig Carter

In[1]:= mylist={{a,b,c},{a,c,b},{b,a,c},{b,c,a},{c,a,b},{c,b,a}}
Out[1]= {{a,b,c},{a,c,b},{b,a,c},{b,c,a},{c,a,b},{c,b,a}}

In[2]:=
neighboring[list_List,a_,b_]:=
Or@@Map[MatchQ[#,Sort[{a,b}]]&,
Map[Sort,Rest[Transpose[{list,RotateRight[list]}]]]];
In[3]:=
notNeighboring[list_List,a_,b_]:=Not[neighboring[list,a,b]];

In[4]:=
Select[mylist,notNeighboring[#,a,b]&]
Out[4]=
{{a,c,b},{b,c,a}}

-------------------
(2) Bob Hanlon

In[5]:=
filter[x_List,n1_,n2_]:=
x/.{{___,n1,n2,___}\[Rule]Sequence[],{___,n2,n1,___}\
[Rule]Sequence[]};

In[6]:=
filter[mylist,a,b]
Out[6]=
{{a,c,b},{b,c,a}}

-------------------
(3) Curtis Osterhoudt

In[7]:=
(MatchQ[#1,{a,_,b}]||MatchQ[#1,{b,_,a}]&)/@mylist // Pick[mylist,#]&
Out[7]=
{{a,c,b},{b,c,a}}

-------------------
(4) Curtis Osterhoudt

In[8]:=
Cases[Cases[mylist,Except[{___,a,b,___}]],Except[{___,b,a,___}]]
Out[8]=
{{a,c,b},{b,c,a}}

-------------------
(5) Carl Wolf

MATHEMATICA 6 REQUIRED

In[9]:= DeleteCases[mylist, {___, PatternSequence[a, b] |
PatternSequence[b, a], ___}]

Out[9]= {{a, c, b}, {b, c, a}}

-------------------
(6) dh

In[10]:=
DeleteCases[mylist,{___,a,b,___}|{___,b,a,___}]
Out[10]=
{{a,c,b},{b,c,a}}

-------------------
(7) Szabolcs Horvat

In[11]:=
Cases[mylist,Except[{___,a,b,___}|{___,b,a,___}]]
Out[11]=
{{a,c,b},{b,c,a}}

-------------------
(8) Jens-Peer Kuska

In[12]:=
Cases[mylist,{a,___,b}|{b,___,a}]
Out[12]=
{{a,c,b},{b,c,a}}

-------------------

• Prev by Date: Cannot NSolve a system of equations
• Next by Date: Re: Problems with ListPlot
• Previous by thread: Re: Filtering a list of list for certain elements that are neighbours
• Next by thread: Redundant manipulate