MathGroup Archive 2010

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

Search the Archive

Re: How do I test for existence of a list element?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg112717] Re: How do I test for existence of a list element?
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Tue, 28 Sep 2010 06:04:09 -0400 (EDT)

On 9/27/10 at 5:48 AM, andre.robin3 at wanadoo.fr (andre.robin3) wrote:

>I recently had the same problem, but for a more general case : How
>to test the existence of a deeply embedded element in a ragged
>structure ?

>Say that the position of the element is specified by : list={i,j,k
>..}

>my solution was something like :
>existenceTestQ[exp_, list_] := Module[{res},
>res = False;
>ReplacePart[exp, list :> (res = True)];
>res
>]

>may be not elegant, but sufficient

=46irst generate a small ragged array

In[5]:= ragged =
  MapThread[
   Take[#1, #2] &, {RandomInteger[{0, 100}, {5, 10}],
    RandomInteger[{1, 10}, 5]}]

Out[5]= {{58,15,40,35,61,97,62,21,64,23},{44,68},{39,49},{43,62,99,10,51,=
4},{1,6,83,6,11,93,89,67}}

And now test for something known to be present:

In[6]:= Length /@ {Position[ragged, 1, Infinity],
   Cases[ragged, {___, 1, ___}], Cases[ragged, 1, Infinity]}

Out[6]= {1,1,1}

And finally test for something known not to be present

In[7]:= Length /@ {Position[ragged, 0, Infinity],
   Cases[ragged, {___, 0, ___}], Cases[ragged, 0, Infinity]}

Out[7]= {0,0,0}

That is either Cases or Position can be used to determine the
presence/absence of something in a ragged array.



  • Prev by Date: Re: Graphics3D without perspective
  • Next by Date: Manipulating Solution List from NDSolve
  • Previous by thread: Re: How do I test for existence of a list element?
  • Next by thread: Re: How do I test for existence of a list element?