MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

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

  • To: mathgroup at smc.vnet.net
  • Subject: [mg88837] Re: Filtering a list of list for certain elements that are neighbours
  • From: Albert Retey <awnl at arcor.net>
  • Date: Mon, 19 May 2008 05:14:25 -0400 (EDT)
  • References: <g0p4a2$nc4$1@smc.vnet.net>

Hi,

> 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}}

I think this is the most straightforward way to achieve what you want:

DeleteCases[mylist, {___, a, b, ___} | {___, b, a, ___}]

> For this toy example, I can probably do it by hand but my real problem
> has 120 lists in a list. And I need to filter out various combination
> of letters like "s cannot be next to d" , "s cannot be next to j" , "c
> cannot be next to j" , "h cannot be next to d" and "h cannot be next
> to s".

There might be more efficient solutions than the above, but for 120
lists I think there is no reason to waste your time to save CPU time.
You might want to write a filter function which can handle arbitrary
pairs, like:

filter[l_,a_,b_]:=DeleteCases[l, {___, a, b, ___} | {___, b, a, ___}]

then use it like:

filter[mylist,s,d]
filter[mylist,s,j]
...

hth,

albert


  • Prev by Date: Re: Filtering a list of list for certain elements that
  • Next by Date: Re: Labeling points
  • Previous by thread: Re: Filtering a list of list for certain elements that are neighbours
  • Next by thread: Re: Filtering a list of list for certain elements that are neighbours