RE: Re: easier method for Flatten[Position[list2,x_x...??
- To: mathgroup at smc.vnet.net
- Subject: [mg94091] RE: [mg94042] Re: easier method for Flatten[Position[list2,x_x...??
- From: "Van Der Burgt, Maarten" <Maarten.VanDerBurgt at icos.be>
- Date: Thu, 4 Dec 2008 07:17:56 -0500 (EST)
- References: <gh0jlg$5mu$1@smc.vnet.net> <200812020540.AAA19072@smc.vnet.net> <200812031042.FAA25012@smc.vnet.net>
Hallo, Thanks to everybody helping me finding a simpler solution. The 'Rule@@@list' solution is indeed very elegant and clever. It also works on more complicated lists: In: {a, b, b, d} /. Rule @@@ {{a, {a1, a2}}, {b, {b1, b2}}, {c, c1},{ d, d1}} =09 Out: {{a1, a2}, {b1, b2}, {b1, b2}, d1} I often use the Rule[lhs, rhs] in the form of Thread[lhlist -> rhlist], which is the same as Thread[Rule[lhlist, rhlist]. Finding back the @@@ is less trivia: pressing F1 for help on @@@ bring you to Apply (@@) where @@@ is explained under the Scope/Level Specifications/ section. In the virtual book @@@ is found in tutorial/ApplyingFunctionsToListsAndOtherExpressions but it is also not trivial to find back. I am not sure if the list with all the shorthand notations like @@, //, ;; etc. we had in 5.2 or older versions is still there in 6 or 7.0.. regards, Maarten -----Original Message----- From: Curtis Osterhoudt [mailto:cfo at lanl.gov] Sent: 03 December 2008 11:42 To: mathgroup at smc.vnet.net Subject: [mg94091] [mg94042] Re: easier method for Flatten[Position[list2,x_x...?? These methods based on the replacement list are very clever! I have long gotten used to the replacements notation, but didn't know it could be used like this. I tried to see if there was any documentation provided with Mathematica (v. 7, here) illustrating anything like that. However, the only documentation I can find on Rule gives the shorthand right-arrow notation "->" and say s NOTHING about the Rule[lhs, rhs] possibility (I had to deduce that from the solutions posted here, and look at the FullForm of some replacements). Can anyone point me to a spot in the documentation where an example using the "Rule[lhs, rhs]" notation is used or explained? If not, it would be a nice improvement in future versions. Regards, C.O. On Monday 01 December 2008 22:40:27 Szabolcs Horv=C3=A1t wrote: > 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? > > Like this: > > In[1]:= > list1 = {a, b, b, e}; > list2 = {{a, aP}, {b, bP}, {c, cP}, {d, dP}, {e, eP}, {f, fP}}; > > In[3]:= list1 /. Rule @@@ list2 > Out[3]= {aP, bP, bP, eP} > > > > > > > > > > > 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} > > > > > > =2D- ========================== = ========================== = ======== Curtis Osterhoudt cfo at remove_this.lanl.and_this.gov PGP Key ID: 0x4DCA2A10 Please avoid sending me Word or PowerPoint attachments See http://www.gnu.org/philosophy/no-word-attachments.html ========================== = ========================== = ========
- References:
- Re: easier method for Flatten[Position[list2,x_x...??
- From: Szabolcs Horvát <szhorvat@gmail.com>
- Re: easier method for Flatten[Position[list2,x_x...??
- From: Curtis Osterhoudt <cfo@lanl.gov>
- Re: easier method for Flatten[Position[list2,x_x...??