Re: Finding shared element in two lists
- To: mathgroup at smc.vnet.net
- Subject: [mg114089] Re: Finding shared element in two lists
- From: Peter Breitfeld <phbrf at t-online.de>
- Date: Tue, 23 Nov 2010 05:59:36 -0500 (EST)
- References: <icdoc5$6ho$1@smc.vnet.net>
Jason Ebaugh 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 I'm not sure if this is the most elegant way, but it works for both of your examples. findMostPrecise[ll:{_List..}]:= Module[{common}, common=Intersection[Sequence@@ll]; Last@Select[ll[[1]],MemberQ[common,#]&] ] findMostPrecise[{mayhouse,friendhouse}] Out= USA findMostPrecise[{aphid,mosquito}] Out= Neoptera -- _________________________________________________________________ Peter Breitfeld, Bad Saulgau, Germany -- http://www.pBreitfeld.de