Re: Finding shared element in two lists
- To: mathgroup at smc.vnet.net
- Subject: [mg114079] Re: Finding shared element in two lists
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Tue, 23 Nov 2010 05:57:38 -0500 (EST)
Hi Jason, This problem seems to be better suited for procedural style. You can do something like this: TakeWhile[Split[Riffle[myhouse, friendshouse]], Length[#] == 2 &][[-1,1]] But this will not be efficient for large lists (not your case, I guess). These versions are slightly more efficient: TakeWhile[Flatten[{myhouse, friendshouse}, {{2}}], Length[#] == 2 && SameQ @@ # &][[-1, 1]] or TakeWhile[ Take[Transpose[#], Min[Length /@ #]] &[{myhouse, friendshouse}], SameQ @@ # &][[-1, 1]] Regards, Leonid On Mon, Nov 22, 2010 at 3:40 PM, Jason Ebaugh <ebaughjason at gmail.com> wrote: > I am trying to write a function that would take two lists of increasing > more > precise location, and return the most specific level of geography that they > share. > > myhouse={"Earth", "North America", "USA", "Illinois", "Champaign County", > "Urbana"}; > friendshouse={"Earth", "North America", "USA", "Minnesota", "Ramsey > County", > "St. Paul"}; > > In this case, the function should output USA. > > I could do this procedurally well enough. But I am really trying to get > comfortable with functional programming. > > > > The above is just a more digestible version of what I am really trying to > do. I want to find the lowest level of taxonomy that two species share. > > Ex: > aphid={"Insecta", "Dicondylia", "Pterygota", "Neoptera", "Paraneoptera", > "Hemiptera", "Sternorrhyncha", "Aphidiformes", "Aphidomorpha", > "Aphidoidea", > "Aphididae", "Aphidinae", "Macrosiphini", "Acyrthosiphon"}; > mosquito={"Insecta", "Dicondylia", "Pterygota", "Neoptera", > "Endopterygota", > "Diptera", "Nematocera", "Culicimorpha", "Culicoidea"}; > > answer = Neoptera > > > Thank a 10^6, > Jason > > > -- > Jason Ebaugh, PhD. > > Personal communications: ebaughjason at gmail.com > Business communications: jason.ebaugh at apisscientific.com > Dissertation: http://hdl.handle.net/2142/13005 > Linkedin: > > http://www.linkedin.com/profile?viewProfile=&key=22842228&locale=en_US&trk=tab_pro > Need bioinformatics help? www.apisscientific.com > >