5

I have the following list:

list1 = {
         {0, 0, 1496.11, 308.593}, {1, 0, 1168.02, 320.9}, {1, 1, 1161.97, 315.926}, 
         {2, 0, 1287.57, 323.834}, {3, 0, 1618.89, 323.874}, {4, 0, 1251.01, 326.575}, 
         {5, 0, 1474.81, 330.69}, {6, 0, 1181.24, 331.692}, {7, 0, 2431.87, 337.616}, 
         {8, 0, 961.105, 340.223}, {9, 0, 1148.98, 344.855}, {9, 1, 1153.03, 336.984}, 
         {10, 0, 1570.98, 346.771}, {11, 0, 1122.66, 349.883}, {11, 1, 1122.34, 351.845}, 
         {11, 2, 1122.63, 353.678}, {11, 3, 1120.75, 351.175}, {11, 4, 1122.24, 358.885}, 
         {11, 5, 1125.73, 360.05}, {11, 6, 1126.43, 362.175}
        }

How can I manupulate list1 to receive list2 ;

list2 = {
         {{0, 0, 1496.11, 308.593}},
         {{1, 0, 1168.02, 320.9}, {1, 1, 1161.97, 315.926}}, 
         {{2, 0, 1287.57, 323.834}}, 
         {{3, 0, 1618.89, 323.874}},
         {{4, 0, 1251.01, 326.575}}, 
         {{5, 0, 1474.81, 330.69}}, 
         {{6, 0, 1181.24, 331.692}}, 
         {{7, 0, 2431.87, 337.616}}, 
         {{8, 0, 961.105, 340.223}}, 
         {{9, 0, 1148.98, 344.855}, {9, 1, 1153.03, 336.984}}, 
         {{10, 0, 1570.98, 346.771}}, 
         {{11, 0, 1122.66, 349.883}, {11, 1, 1122.34, 351.845}, {11, 2, 1122.63, 353.678}, 
          {11, 3, 1120.75, 351.175}, {11, 4, 1122.24, 358.885}, {11, 5, 1125.73, 360.05}, 
          {11, 6, 1126.43, 362.175}}
        }

I tried to use a Do loop with If conditions and AppendTo but the code became terribly complicated.

lio
  • 2,396
  • 13
  • 26

1 Answers1

8
GatherBy[list1, First] (* or *)
Values @ GroupBy[list1, First] (* or *)
Values @ Merge[Identity][Association[{# -> {##}}]& @@@ list1] 

{{{0, 0, 1496.11, 308.593}},
{{1, 0, 1168.02, 320.9}, {1, 1, 1161.97, 315.926}},
{{2, 0, 1287.57, 323.834}},
{{3, 0, 1618.89, 323.874}},
{{4, 0, 1251.01, 326.575}},
{{5, 0, 1474.81, 330.69}},
{{6, 0, 1181.24, 331.692}},
{{7, 0, 2431.87, 337.616}},
{{8, 0, 961.105, 340.223}},
{{9, 0, 1148.98, 344.855}, {9, 1, 1153.03, 336.984}},
{{10, 0, 1570.98, 346.771}},
{{11, 0, 1122.66, 349.883}, {11, 1, 1122.34, 351.845}, {11, 2, 1122.63, 353.678}, {11, 3, 1120.75, 351.175}, {11, 4, 1122.24, 358.885}, {11, 5, 1125.73, 360.05}, {11, 6, 1126.43, 362.175}}}

kglr
  • 394,356
  • 18
  • 477
  • 896
  • oh no ... I was sure that something short would come out but this is depressing ;-) Thanks a lot. – lio Jun 29 '18 at 16:19
  • @lio, my pleasure:) – kglr Jun 29 '18 at 16:28
  • Would you consider this a duplicate of (4332), or (66286)? Each is a bit more complicated but this simpler problem is solved in the process. – Mr.Wizard Jun 30 '18 at 16:07
  • @Mr.Wizard, I am not sure how to treat this kind of cases. Besides. this one feels so familiar that I am almost sure there is a more direct duplicate, but I couldn't find them with keywords GroupBy, GatherBy and Merge. – kglr Jun 30 '18 at 22:30
  • 1
    I looked too, and those were the best I could find. Maybe simpler questions got closed and deleted as Easily found in the documentation? By the way, any reason you don't lead your answer with GatherBy? – Mr.Wizard Jul 01 '18 at 05:50
  • @Mr.Wizard, I made the change (there was no reason:). – kglr Jul 01 '18 at 05:57