Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

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


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

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

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Dave Wagner

Posts: 19
Registered: 12/7/04
Re: How do I use Select[list,condition]?
Posted: Jul 30, 1996 2:59 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply


In article <4thatm$j6e@dragonfly.wolfram.com>,
Jason Welter <welter@texoma.com> 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?


The argument to the pure function in Select is an individual element of the
list being selected, not an index. Thus, the test you want is #[[2]]==2&.

(Local) In[15]:=
Select[data, #[[2]]==2&]
(Local) Out[15]=
{{1, 2, b, 4, 5, 6}, {2, 2, b, 2, 3, 4}, {3, 2, b, 7, 8, 9}}

It's eve easier to do this using Cases and a pattern:

(Local) In[16]:=
Cases[data, {_,2,__}]
(Local) Out[16]=
{{1, 2, b, 4, 5, 6}, {2, 2, b, 2, 3, 4}, {3, 2, b, 7, 8, 9}}

In my opinion, this code is easier to understand at a glance --
the pattern is quite explicit.

Dave Wagner
Principia Consulting
(303) 786-8371
dbwagner@princon.com
http://www.princon.com/princon







Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.