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: [mg115003] Re: newbie list question
  • From: Peter Pein <petsie at dordos.net>
  • Date: Tue, 28 Dec 2010 06:49:05 -0500 (EST)
  • References: <if70bt$330$1@smc.vnet.net>

On 26.12.2010 10:02, 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!
>

Hi Gareth,


Use Position to get the first location of a number in level 1 of listA
Position[listA,number,1,1]
this will give {{pos. of nr.}}.
Now build an anonymous function and map that on listB. Flatten the 
result to get rid of unnecessary nested lists:
Position[listA, #1, 1, 1] & /@ listB // Flatten
wil give {3,1,4} as result.


Regards,
  Peter


  • 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