Re: A list based table join?
- To: mathgroup at smc.vnet.net
- Subject: [mg112964] Re: A list based table join?
- From: Patrick Scheibe <pscheibe at trm.uni-leipzig.de>
- Date: Fri, 8 Oct 2010 04:49:09 -0400 (EDT)
Hi, you *could*: - gather all elements of x by the words, so that you have elements with the same word collected - transform this into a list of rules where each rule looks like "word"->{num1,...} - use this to replace the words in y selectItems[x_, y_] := Flatten[y /. ((Transpose /@ Gather[x, First@#1 === First@#2 &]) /. {{word_String ..}, vals : {__Integer}} :> Rule[word, vals])] x = {{"hello", 27}, {"bye", 30}, {"hello", 54}, {"seeya", 100}, {"hi", 9}}; y = {"hello", "hi"}; selectItems[x,y] Cheers Patrick On Thu, 2010-10-07 at 03:39 -0400, Leo Ditolaghi wrote: > Hi all, > > (noob Mathematica question:) > > If I have two lists, how do I determine the indices at which the 1st list > elements are matched by the elements of the second, and then use these > matching indices to extract corresponding columns in the 1st list (sort of a > simulated database join)? I.e., > > x = {{"hello", 27}, > {"bye", 30}, > {"hello", 54}, > {"seeya", 100}, > {"hi", 9}} > > y = {"hello", "hi"} > > where x is the source table, and y is the select criteria (so to speak.) > > I wish to produce z, where y defines which elements of x to extract: > > z = {27,54,9} > > I know this should be easy and elegant, but I am going in circles > with list functions. > > Thanks in advance, > > Leo > >