Re: Using Part
- To: mathgroup at smc.vnet.net
- Subject: [mg77094] Re: [mg77057] Using Part
- From: DrMajorBob <drmajorbob at bigfoot.com>
- Date: Sun, 3 Jun 2007 06:12:42 -0400 (EDT)
- References: <18064701.1180782141199.JavaMail.root@m35>
- Reply-to: drmajorbob at bigfoot.com
Select operates on one top-level element at a time, so #2 is undefined. Try Split, instead. data = {{16871, I7, 614, 48876, 850, {2006, 12, 2}}, {16871, I7, 538.3, 49803, 850, {2007, 1, 8}}, {16871, I7, 474.8, 50655, 850, {2007, 2, 1}}, {16871, I7, 428.1, 51430, 850, {2007, 2, 20}}, {16871, I7, 74.9, 51430, 850, {2007, 2, 20}}, {16871, I7, 535, 52348, 850, {2007, 3, 20}}, {16871, I7, 395.9, 53383, 850, {2007, 5, 9}}}; split = Split[SortBy[data, Last], Last[#1] == Last[#2] &]; sums = Total[#[[All, 3]]] & /@ %; result = First /@ split; result[[All, 3]] = sums; result {{16871, I7, 614, 48876, 850, {2006, 12, 2}}, {16871, I7, 538.3, 49803, 850, {2007, 1, 8}}, {16871, I7, 474.8, 50655, 850, {2007, 2, 1}}, {16871, I7, 503., 51430, 850, {2007, 2, 20}}, {16871, I7, 535, 52348, 850, {2007, 3, 20}}, {16871, I7, 395.9, 53383, 850, {2007, 5, 9}}} SortBy is unnecessary if the list is already sorted. I could have used #1[[6]]==#2[[6]]& as the Split criterion, but I liked Last a bit better. Bobby On Sat, 02 Jun 2007 03:15:18 -0500, Clifford Martin <camartin at snet.net> wrote: > Group, > > I need some educational help. Below is a list of lists I'm trying to > manipulate. Notice that the date is in the 6th place in each list. What > I want to do is 1) identify the lists with identical dates and 2) once > I've done that add the third element of each list with identical dates > together and 3) eliminate the two original lists. > > {{{16871,I7,614,48876,850,{2006,12,2}},{16871,I7,538.3,49803,850,{2007,1,8}}, > {16871,I7,474.8,50655,850,{2007,2,1}},{16871,I7,428.1,51430,850,{2007,2,20}}, > {16871,I7,74.9,51430,850,{2007,2,20}},{16871,I7,535,52348,850,{2007,3,20}}, > {16871,I7,395.9,53383,850,{2007,5,9}}} > > So what I would end up with would be: > > {{{16871,I7,614,48876,850,{2006,12,2}},{16871,I7,538.3,49803,850,{2007,1,8}}, > {16871,I7,474.8,50655,850,{2007,2,1}},{16871,I7,503,51430,850,{2007,2,20}}, > {16871,I7,535,52348,850,{2007,3,20}},{16871,I7,395.9,53383,850,{2007,5,9}}} > There is something about using Part that I'm not understanding. In the > list below if I say: > Part[list,4,6]==Part[list,5,6] I get > True > but if I try to find it with an anonymous function like > Select[list,Part[#1,6]==Part[#2,6] this doesn't find it. Clearly I'm > not understanding how to use these functions with Part. I've tried many > variations of this and it just shows I'm not understanding something > fundamental. > Thanks for your help in advance. > > Cliff > > -- DrMajorBob at bigfoot.com