Re: Using Part
- To: mathgroup at smc.vnet.net
- Subject: [mg77091] Re: Using Part
- From: Bill Rowe <readnewsciv at sbcglobal.net>
- Date: Sun, 3 Jun 2007 06:11:09 -0400 (EDT)
On 6/2/07 at 4:15 AM, camartin at snet.net (Clifford Martin) 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}}} Use Split to group items with like dates and ReplacePart to replace the 3rd element of the first item in each group with the desired sum, i.e. In[24]:= list = {{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}}}; In[25]:= (ReplacePart[First[#1], {3 -> Total[#1[[All, 3]]]}] & ) /@ Split[list, Last[#1] == Last[#2] & ] Out[25]= {{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}}} Note, in the example you gave each sublist is 6 elements long. So, your same dates in the 6th position is the same as same dates in the last position. Also in the example you gave, items with same dates are in consecutive order. If either of these aren't true in general than the portion using Split would need to be modified -- To reply via email subtract one hundred and four