7
$\begingroup$

I am trying to manage a matrix in Mathematica that has the general architecture

{{a1,b1},{a2,b2}...}

I would like to combine all of the lists that have the same the first value and transform it to a new list of lists of the second values,

For example, If I have the list {{1,2},{1,5},{1,30},{2,6},{3,8},{3,30},{4,1}}

I would like the output to be {{2,5,30},{6},{8,30},{1}}

I would then like to remove any of the list elements that contain only 1 value

So that my final output would be {{2,5,30},{8,30}}

I am still new to data manipulation in mathematica and really do not know where to begin on this problem

$\endgroup$

1 Answer 1

8
$\begingroup$

Use GroupBy:

Values @ DeleteCases[{_}] @ GroupBy[
    {{1,2},{1,5},{1,30},{2,6},{3,8},{3,30},{4,1}},
    First->Last
]

{{2, 5, 30}, {8, 30}}

or GatherBy

DeleteCases[{_}] @ GatherBy[
    {{1,2},{1,5},{1,30},{2,6},{3,8},{3,30},{4,1}},
    First
][[All, All, 2]]

{{2, 5, 30}, {8, 30}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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