MathGroup Archive 2007

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

Search the Archive

Fwd: Just one "1" in a list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg78260] Fwd: Just one "1" in a list
  • From: "Jean-Marc Gulliet" <jeanmarc.gulliet at gmail.com>
  • Date: Wed, 27 Jun 2007 05:24:05 -0400 (EDT)
  • References: <f5j11r$d0m$1@smc.vnet.net> <467D1553.3090501@gmail.com>

Diana wrote:
> Math folks,
>
> Can someone tell me what command I would use to pick out sublists
> which contain just one "1"?
>
> For example, if I have the following list,
>
> {{1,2}, {1,3}, {2,3}, {2,1}, {1,1,2}, {1,2,2}, {2,1}},
>
> I would like to just pull the terms,
>
> {{1,2}, {1,3}, {2,1}, {1,2,2}, {2,1}}.
>
> Can someone help? Thanks, Diana M.

Hi Diana,

One possible way is the following: we count the number of 1s in each
sub-list and select the list with exactly one occurrence of 1.

lst = {{1, 2}, {1, 3}, {2, 3}, {2, 1}, {1, 1, 2}, {1, 2, 2}, {2, 1}};

Pick[lst, (Count[#1, 1] & ) /@ lst, 1]

==> {{1, 2}, {1, 3}, {2, 1}, {1, 2, 2}, {2, 1}}

That is (in two stages)

In[2]:=
sel = (Count[#1, 1] & ) /@ lst

Out[2]=
{1, 1, 0, 1, 2, 1, 1}

In[3]:=
Pick[lst, sel, 1]

Out[3]=
{{1, 2}, {1, 3}, {2, 1}, {1, 2, 2}, {2, 1}}

Regards,
Jean-Marc


  • Prev by Date: Re: Plot in a Do loop does nothing
  • Next by Date: Re: Plot in a Do loop does nothing
  • Previous by thread: Re: Just one "1" in a list
  • Next by thread: Re: Just one "1" in a list