Re: Clever way to manipulate lists
- To: mathgroup at smc.vnet.net
- Subject: [mg94418] Re: [mg94366] Clever way to manipulate lists
- From: DrMajorBob <btreat1 at austin.rr.com>
- Date: Fri, 12 Dec 2008 06:56:24 -0500 (EST)
- References: <200812110845.DAA01163@smc.vnet.net>
- Reply-to: drmajorbob at longhorns.com
Here's an example where both lists have deletions: one = Drop[Array[{#, #} &, 10], {3}] two = Drop[Array[{#, #^2} &, 10], {2, 5}] {{1, 1}, {2, 2}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}, {10, 10}} {{1, 1}, {6, 36}, {7, 49}, {8, 64}, {9, 81}, {10, 100}} and here's a simple way of pruning them to common x values: oneMod = With[{xVals = two[[All, 1]]}, Select[one, MemberQ[xVals, #[[1]]] &]] twoMod = With[{xVals = one[[All, 1]]}, Select[two, MemberQ[xVals, #[[1]]] &]] {{1, 1}, {6, 6}, {7, 7}, {8, 8}, {9, 9}, {10, 10}} {{1, 1}, {6, 36}, {7, 49}, {8, 64}, {9, 81}, {10, 100}} or oneMod = Intersection[one, two, SameTest -> (Equal @@ First /@ {##} &)] twoMod = Intersection[two, one, SameTest -> (Equal @@ First /@ {##} &)] {{1, 1}, {6, 6}, {7, 7}, {8, 8}, {9, 9}, {10, 10}} {{1, 1}, {6, 36}, {7, 49}, {8, 64}, {9, 81}, {10, 100}} Note: I've seen no documentation to suggest those Intersections should be different. But they are, fortunately! Also, both methods depend (to some extent) on having no duplicates among the x values. Bobby On Thu, 11 Dec 2008 02:45:28 -0600, guerom00 <guerom00 at gmail.com> wrote: > 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 :) > -- DrMajorBob at longhorns.com
- References:
- Clever way to manipulate lists
- From: guerom00 <guerom00@gmail.com>
- Clever way to manipulate lists