I have a list of lists like this one, where the sublists vary in length.
list = {{0, 1, 2}, {0, -2, 4}, {0, 3, 6}, {1, 2, 3}, {0, 0}, {2, 4, 6}, {-2, -4, -6}, {1, 2, -3}, {1, -2, 3}, {-6, -8, -10}, {0.3, 0.4, 0.5}, {0, 0}};
I want to rearrange the list so that each group has all the multiples of the sublists grouped together. The above example should give this reorganized list:
{{0, 1, 2}, {0, 3, 6}}
{{0, -2, 4}}
{{1, 2, 3}, {2, 4, 6}, {-2, -4, -6}}
{{0, 0}, {0, 0}}
{{1, 2, -3}}
{{1, -2, 3}}
{{-6, -8, -10}, {0.3, 0.4, 0.5}}
I have read this post, but it’s mainly suitable for positive integer multiples and equal-length sublists. This method fails with my example.
GatherBy[list, #/Max[1, GCD @@ #] &]
I prefer the solution based on GatherBy
instead of Gather
, because GatherBy
is much more efficient.