Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: How do I use Select[list,condition]?
Replies: 7   Last Post: Aug 2, 1996 2:04 AM

 Messages: [ Previous | Next ]
 Stephen P Luttrell Posts: 121 Registered: 12/7/04
Re: How do I use Select[list,condition]?
Posted: Jul 30, 1996 2:57 AM

Jason Welter wrote:
>
> I have a list that looks like:
>
> data = {{1,1,a,4,5,6},{2,1,a,2,3,4},{3,1,a,7,8,9},
> {1,2,b,4,5,6},{2,2,b,2,3,4},{3,2,b,7,8,9},
> {1,3,c,4,5,6},{2,3,c,2,3,4},{3,3,c,7,8,9}}
>
> and I want to pick out the lists that have a 2 as the second element
> of the list. I tried something like:
>
> data1 = Select[data,data[[#,2]]==2];
>
> but this doesn't work. How do I do it?

Try the following:

data = {{1,1,a,4,5,6},{2,1,a,2,3,4},{3,1,a,7,8,9},
{1,2,b,4,5,6},{2,2,b,2,3,4},{3,2,b,7,8,9},
{1,3,c,4,5,6},{2,3,c,2,3,4},{3,3,c,7,8,9}};
Select[data, MatchQ[#, {_,2,___}]&]

which produces the output

{{1, 2, b, 4, 5, 6}, {2, 2, b, 2, 3, 4}, {3, 2, b, 7, 8, 9}}

--
Dr Stephen P Luttrell luttrell@signal.dra.hmg.gb
Room EX21, Defence Research Agency 01684-894384 (fax)
Malvern, Worcs, WR14 3PS, U.K. http://www.dra.hmg.gb/cis5pip/Welcome.html

Date Subject Author
7/28/96 Jason Welter
7/30/96 Stephen P Luttrell
7/30/96 Dave Wagner
7/30/96 Elizabeth J Earhart
7/30/96 Harald Berndt
7/30/96 Nate Brixius
7/30/96 Wolfram Jr.
8/2/96 Hein Hundal