4
$\begingroup$

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.

$\endgroup$
  • 1
    $\begingroup$ Another one, using that your list is sorted: Split[list1, First@#1 == First@#2 &] $\endgroup$ – Fred Simons Jun 29 '18 at 16:42
  • $\begingroup$ yes it is sorted according first element of sublists $\endgroup$ – lio Jun 29 '18 at 17:04
8
$\begingroup$
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}}}

| improve this answer | |
$\endgroup$
  • $\begingroup$ oh no ... I was sure that something short would come out but this is depressing ;-) Thanks a lot. $\endgroup$ – lio Jun 29 '18 at 16:19
  • $\begingroup$ @lio, my pleasure:) $\endgroup$ – kglr Jun 29 '18 at 16:28
  • $\begingroup$ 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. $\endgroup$ – Mr.Wizard Jun 30 '18 at 16:07
  • $\begingroup$ @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. $\endgroup$ – kglr Jun 30 '18 at 22:30
  • 1
    $\begingroup$ 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? $\endgroup$ – Mr.Wizard Jul 1 '18 at 5:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.