RE: Clever way to manipulate lists
- To: mathgroup at smc.vnet.net
- Subject: [mg94425] RE: [mg94366] Clever way to manipulate lists
- From: "Scot Martin" <smartin at seas.harvard.edu>
- Date: Fri, 12 Dec 2008 06:57:40 -0500 (EST)
- Organization: Harvard University
- References: <200812110845.DAA01163@smc.vnet.net>
- Reply-to: <scot_martin at harvard.edu>
Not sure if this is what you're looking for, but it works: In[1]:= list1 = {{x1, y1}, {x2, y2}, {x3, y3}, {xN, yN}}; list2 = {{x1, z1}, {x3, z3}, {x4, z4}, {xN, zN}}; masterlist = Intersection[First /@ list1, First /@ list2]; smalllist1 = DeleteCases[list1, {_?(! MemberQ[masterlist, #] &), _}]; smalllist2 = DeleteCases[list2, {_?(! MemberQ[masterlist, #] &), _}]; In[6]:= smalllist1 Out[6]= {{x1, y1}, {x3, y3}, {xN, yN}} In[7]:= smalllist2 Out[7]= {{x1, z1}, {x3, z3}, {xN, zN}} -----Original Message----- From: guerom00 [mailto:guerom00 at gmail.com] Sent: Thursday, December 11, 2008 3:45 AM To: mathgroup at smc.vnet.net Subject: [mg94425] [mg94366] Clever way to manipulate lists Hi everyone, I'm still struggling through lists manipulation. I'll take a concrete example to illustrate my point. Let's say I have a first list, say coordinates on a regular grid : list1={{x1,y1},{x2,y2},{x3,y3}...{xN,yN}} This obviously has a Length of N. Now, let's say I have a second list. In this one, there are fewer than N elements, some points are missing... Let's say it misses a point at x2 : list2 ={{x1,z1},{x3,z3},{x4,z4}...{xN,zN}} Now, since those two lists are not of the same length, I cannot add them, substract them or something. But list2 is included in list1 (in the sense of set theory). Now, what I want to do is, in this example, remove the point {x2,y2} from list1 and then the two list will have the same length and I'll be able to manipulate them as I want. Right now, I do that with For loops (detect elements which are in list1 and not in list2 and delete them, etc...) and that works but it is not elegant. I'm looking for a concise, elegant way to do that if somebody sees what I mean... Thanks in advance :)
- References:
- Clever way to manipulate lists
- From: guerom00 <guerom00@gmail.com>
- Clever way to manipulate lists