Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: newbie list question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg114988] Re: newbie list question
  • From: Patrick Scheibe <pscheibe at trm.uni-leipzig.de>
  • Date: Tue, 28 Dec 2010 06:46:21 -0500 (EST)

Hi,

the long and readable version is

listA = {4, 5, 8, 2, 6, 4};
listB = {8, 4, 2};
Function[{searchItem}, First[Position[listA, searchItem]]] /@ listB

but there's no need to use the verbose form of Function:

In[13]:= listA = {4, 5, 8, 2, 6, 4};
listB = {8, 4, 2};
First@Position[listA, #] & /@ listB

Out[15]= {{3}, {1}, {4}}

If you don't like that Position gives a list of possible positions of
each element you can use Flatten to flatten out the list.

Cheers
Patrick


On Sun, 2010-12-26 at 04:02 -0500, Gareth Edwards wrote:
> Hi,
> 
> 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)
> 
> Many thanks!
> 



  • Prev by Date: Re: newbie list question
  • Next by Date: Re: newbie list question
  • Previous by thread: Re: newbie list question
  • Next by thread: Re: newbie list question