Re: easier method for Flatten[Position[list2,x_x...??
- To: mathgroup at smc.vnet.net
- Subject: [mg94030] Re: [mg93990] easier method for Flatten[Position[list2,x_x...??
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Tue, 2 Dec 2008 00:43:41 -0500 (EST)
- References: <200812011200.HAA05857@smc.vnet.net>
On 1 Dec 2008, at 21:00, Van Der Burgt, Maarten wrote:
> Dear all,
>
> Below I have two lists.
>
> The elements x in list1 all appear again in the 2nd level of list2
> together with an associated parameter xP.
>
> I want to extract the xP as illustrated below.
>
> I have the feeling it can be done in an easier way.
>
>
>
> Does anyone have an idea how?
>
>
>
> Thanks for your help,
>
>
>
> Maarten
>
>
>
>
>
> list1={a,b,b,e};
>
>
>
> list2={{a,aP},{b,bP},{c,cP},{d,dP},{e,eP},{f,fP}};
>
>
>
>
>
> pos = Flatten[Position[list2,x_List/;x[[1]]==#]&/@list1];
>
>
>
> list2[[pos,2]]
>
>
>
> Out-> {aP,bP,bP,eP}
>
>
I don't know about easier but it is certainly different:
list1 /. Thread[DeleteDuplicates[list1] -> Extract[list2,
Position[list2, Alternatives @@ list1] /. {i_, 1} -> {i, 2}]]
{aP, bP, bP, eP}
Note that this actually needs Mathematica 7 to work.
Andrzej Kozlowski
- References:
- easier method for Flatten[Position[list2,x_x...??
- From: "Van Der Burgt, Maarten" <Maarten.VanDerBurgt@icos.be>
- easier method for Flatten[Position[list2,x_x...??