Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1997

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

Search the Archive

Re: selecting from lists

  • To: mathgroup at
  • Subject: [mg6132] Re: [mg6120] selecting from lists
  • From: Allan Hayes <hay at>
  • Date: Sun, 23 Feb 1997 00:10:45 -0500
  • Sender: owner-wri-mathgroup at

murray at (Murray Eisenberg)
[mg6120] selecting from lists

> given an arbitrary list v and a list b of the same length, whose
> entries are all Boolean (True or False), construct a function
> s[v, b] whose result is a list consisting only of those entries  
of > v for which the corresponding entries in b are True.
> For example,
>   v = {3, 82, 7, -12, 5};
>   b = {False, True, True, False};
>   s[v, b]
>	{82, 7}

> construct a function firstnonzero that returns the index of the
> first nonzero entry in a list (or, more generally, the first  
entry > in a list satisfying a given property).

Here are some ideas:
Both functions , s and p, allow the first n qualifying entries to  
be chosen. We can easily go further, in particular by allowing s and  
p to pass level specifications.

s[v_,b_,n_:Infinity]:= Cases[Thread[{v,b}], {x_,True}:>x,1,n]

	{82, 7}


p[v_, pattern_, n_:Infinity] := Position[v, pattern, 1, n]

p[{1,2,1,2,2,1}, 1]
	{{1}, {3}, {6}}

p[{1,2,1,2,2,1}, 1, 2]
	{{1}, {3}}
Allan Hayes
hay at

  • Prev by Date: Re: selecting from lists
  • Next by Date: Re: Version of Mathematica
  • Previous by thread: Re: selecting from lists
  • Next by thread: Re: selecting from lists