I have a list of pairs of numbers and I want to group these pairs in such a way that in each group, the second element of each item is equal to the first element of the next one. For example, consider this list:
list = {{-1, -2}, {-1, 1.4}, {-2, -2.24}, {-3, -4}, {-5, -6}, {-5, -5.08}, {-6, 1},
{1, 1.4}, {1, 2}, {-2.24, -3.16}, {-3.16, -3.6}, {-4.12, -5.08}, {-4.12, -4.48},
{2.24, 2.84}, {2.24, 3.16}, {2.84, 3.6}, {-3.6, -4.48}, {-5.4, -6.32}, {-5.4, -5.84},
{-6.32, -6.72}, {3.16, 3.6}, {4.24, -5}, {-5.84, -6.72}};
which can be divided into 12 groups like this:
{{ {-1, -2}, {-2, -2.24}, {-2.24, -3.16}, {-3.16, -3.6}, {-3.6, -4.48} },
{ {-1, 1.4} },
{ {-3, -4} },
{ {-5, -6}, {-6, 1}, {1, 1.4} },
{ {4.24, -5}, {-5, -5.08} },
{ {1, 2} },
{ {-4.12, -5.08} },
{ {-4.12, -4.48} },
{ {2.24, 2.84}, {2.84, 3.6} },
{ {2.24, 3.16}, {3.16, 3.6} },
{ {-5.4, -6.32}, {-6.32, -6.72} },
{ {-5.4, -5.84}, {-5.84, -6.72} }}
My current approach involves looping through all elements and removing each one from the main list after Append
ing it to its designated group, which is a highly inefficient and dirty code. Considering that the list might become too big, is there a fast and efficient way to do this in Mathematica?
{{4.24, -5}, {-5, -5.08}}
and{{4.24, -5}, {-5, -6}, {-6, 1}, {1, 1.4}}
in your list? $\endgroup$