MathGroup Archive 2009

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

Search the Archive

Re: find subsets

  • To: mathgroup at
  • Subject: [mg104736] Re: find subsets
  • From: Helen Read <hpr at>
  • Date: Sun, 8 Nov 2009 06:47:22 -0500 (EST)
  • References: <hd3mn9$9l4$>
  • Reply-to: HPR <read at>

r_poetic wrote:
> Hello,
> here is a quick question from a novice.
> First, generate a list of subsets using Subsets or KSubsets
> functions.
> Ns = KSubsets[{a,b,c,d,e},3] = {{a,b,c},{a,b,d},{a,b,e},{a,c,d}, etc.}

It's a good practice to use names that begin with a lower case letter, 
to avoid conflict with the names of built-in symbols and functions.

> How can one infer from that list a list of its members that contain
> given elements, e.g. a list of members that contain b, or those that
> contain both b and c? For illustration:
> Some_function[Ns, b and c] = { {a,b,c},{b,c,d},{b,c,e}}
> I keep thinking there must be a direct way to do this using Select or
> some other function?

Select together with MemberQ will do the job. First, generate the subsets:

sets = Subsets[{a, b, c, d, e}, {3}]

This will select the ones that contain b:

Select[sets, MemberQ[#, b] &]

This will select the ones that contain both b and c:

Select[sets, (MemberQ[#, b]) && (MemberQ[#, c]) &]

And this will select the ones that contain b or c:

Select[sets, (MemberQ[#, b]) || (MemberQ[#, c]) &]

Helen Read
University of Vermont

  • Prev by Date: Re: Function returns null
  • Next by Date: Re: Plot3d causes crash with radeon driver and Debian testing
  • Previous by thread: Re: find subsets
  • Next by thread: Re: find subsets