Re: newbie list question
- To: mathgroup at smc.vnet.net
- Subject: [mg114996] Re: newbie list question
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Tue, 28 Dec 2010 06:47:48 -0500 (EST)
On 12/26/10 at 4:02 AM, gareth.edwards at cubicmotion.com (Gareth
Edwards) wrote:
>Liking Mathematica a lot, but struggling with the early part of the
>learning curve, i.e. don't know what I don't know...
>What would be the neatest syntax for finding the first location of
>elements 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
>location in A of each element in B)
Neatest will always be a matter of opinion. Here is one way
In[4]:= listA = {4, 5, 8, 2, 6, 4};
listB = {8, 4, 2};
Position[listA, #][[1, 1]] & /@ listB
Out[6]= {3,1,4}
Here I am using the /@ (shorthand for Map) to map a pure
function, the Position[listA,#][[1,1]]& part, to each element of listB