1
$\begingroup$

Original Question: I have a list {{a, 1}, {b, 3}, {c, 1}, {d, 2}, {e, 3}}. I want to be able to group the the first number in each pair that has the same second number in the pair. So i would get a list like

{{a,c},d,{b,e}} so

{a,c} is from {a,1} and {c,1}

d is from {d,2}

{b,e} is from {b,3} and {e,3}

$\endgroup$

closed as off-topic by Quantum_Oli, xyz, user9660, Mr.Wizard Jun 17 '16 at 9:48

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Quantum_Oli, xyz, Community, Mr.Wizard
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    $\begingroup$ If you have V10+ take a look at GroupBy, otherwise GatherBy documentation. $\endgroup$ – Kuba Jun 17 '16 at 7:12
  • $\begingroup$ See also (4332) and its many Linked questions. $\endgroup$ – Mr.Wizard Jun 17 '16 at 9:48
  • $\begingroup$ Your update seems to change the question substantially. (It makes the answer that you accepted incorrect.) You should instead ask a new question. (BTW, it appears that answer to your new question is DeleteCases[0] /@ Transpose[matrix].) $\endgroup$ – Michael E2 Jun 20 '16 at 12:50
  • 1
    $\begingroup$ Transpose[matrix] /. 0 -> Nothing $\endgroup$ – m_goldberg Jun 20 '16 at 14:09
2
$\begingroup$

I think I know what you need:

SortBy[GatherBy[list,Last],Last@*Last][[;;,;;,1]]/.{x_?AtomQ}->x

The result is:

{{a,c},d,{b,e}}

This will gather the elements by the last member of each small list, sort it by the index number and then throw away all the index numbers. Finally, it can change all one element list into the number itself.

Will this help directly?

$\endgroup$
  • $\begingroup$ @HighPerformanceMark oh yes! I mistype it cause I'm currently typing with my tiny phone on my hand. Thanks for informing! $\endgroup$ – Wjx Jun 17 '16 at 7:32
2
$\begingroup$

I am not sure I understand. Here are some ways to group by last element:

list={{1, 1}, {1, 3}, {2, 1}, {2, 2}, {2, 3}}
GatherBy[list, Last]
GroupBy[list, Last]
Last@Reap[Sow[{##}, #2] & @@@ list, _, Rule]

yielding respectively:

{{{1, 1}, {2, 1}}, {{1, 3}, {2, 3}}, {{2, 2}}}

<|1 -> {{1, 1}, {2, 1}}, 3 -> {{1, 3}, {2, 3}}, 2 -> {{2, 2}}|>

{1 -> {{1, 1}, {2, 1}}, 3 -> {{1, 3}, {2, 3}}, 2 -> {{2, 2}}}

GroupBy has other useful features.

$\endgroup$
  • 1
    $\begingroup$ Probably GroupBy[list, Last -> First] with skiping {} from one element lists. Not so sure about an order and its meaning. $\endgroup$ – Kuba Jun 17 '16 at 7:21
  • $\begingroup$ I don't mind the extra {}. But how do i use this as a list it has the weird form <|1 -> {1, 2}, 3 -> {1, 2}, 2 -> {2}|> $\endgroup$ – MTR Jun 17 '16 at 7:28
  • $\begingroup$ @MTR that is an Association (see documentation), You can "normalize" it with Normal@GroupBy[list,Last]. $\endgroup$ – ubpdqn Jun 17 '16 at 7:31
  • $\begingroup$ @MTR or Values @ GroupBy[list, Last -> First] $\endgroup$ – Kuba Jun 17 '16 at 7:34

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