Re: newbie list question
- To: mathgroup at smc.vnet.net
- Subject: [mg115002] Re: newbie list question
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Tue, 28 Dec 2010 06:48:54 -0500 (EST)
- References: <if70bt$330$1@smc.vnet.net>
Hi Gareth, Assuming, that all elements from the second list are present in the first, here is a simple way: Flatten[Map[Position[listA, #, 1, 1] &, listB]] For large listA and listB, this solution does not scale well however. Here is another one, perhaps a bit more obscure, but with a much better performance: listB /. Dispatch[Thread[listA -> Range[Length[listA]]]] Again, both solutions assume that all elements in listB are also in listA. Hope this helps. Regards, Leonid On Dec 26, 12:02 pm, Gareth Edwards <gareth.edwa... at cubicmotion.com> wrote: > Hi, > > Liking Mathematica a lot, but struggling with the early part of the learn= ing curve, i.e. don't know what I don't know... > > What would be the neatest syntax for finding the first location of elemen= ts from one list in another? For example: > > listA = { 4,5,8,2,6,4 } > listB = { 8,4,2 } > > I would like a function to return {3,1,4} in this case (the first locatio= n in A of each element in B) > > Many thanks!