Re: Function pure for Select
- To: mathgroup at smc.vnet.net
- Subject: [mg93102] Re: Function pure for Select
- From: MattAd <adereth at gmail.com>
- Date: Sun, 26 Oct 2008 01:29:05 -0500 (EST)
- References: <gdugaf$jpj$1@smc.vnet.net>
On Oct 25, 3:02 am, Bob Hanlon <hanl... at cox.net> wrote: > Split[Sort[Select[data, > MemberQ[{30, 45, 50, 66}, #[[1]]] &]], > #1[[1]] == #2[[1]] &] > > However, it is much easier to use a helper function: > > f[x_] := Select[data, #[[1]] == x &] > > f /@ {30, 45, 50, 66} > > Bob Hanlon > > ---- Miguel <misv... at gmail.com> wrote: > > ============= > Hi al, > > How can I to write a function pure to extract all the first rows of > collection of data, applied to a list?. > > For example, > > Select[data,First[#]==30&] > > This function extracts all rows which first element is equal to 30. > Well, I want to extend this function to a list of values > {30,45,50,66}. > > Thanks > > -- > > Bob Hanlon Here's a look at a few approaches, with timings on my machine: data = Table[{RandomInteger[1000], RandomInteger[1000]}, {100000}]; targets = Table[i, {i, 250}]; Select[data, MemberQ[targets, First[#]] &] ...takes 2.886 seconds f[x_] := Select[data, First[#] == x &]; f /@ targets ...takes 93.616 seconds Cases[data, {Alternatives@@targets, _}] ...takes 1.061 seconds IsTarget[_] := False; Scan[(IsTarget[#] = True) &, targets]; Select[data, IsTarget[First[#]] &] ...takes 0.452 seconds