Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a list, say,

cc = {{0.16441114380905922, 1}, {0.16441114380905922, 2},
      {-0.16441114381135857, 4}, {0.3469323902621586, 3},
      {-0.3469323902254179, 2}, {-0.26126804383825986, 3},
      {0.2612680438599802, 2}, {-0.28969854537720147, 3},
      {0.28969854538168743, 2}, {-7.968690709137364, 3},
      {7.968690708910122, 2}, {-4.000091496006696, 3},
      {4.000091495616817, 2}, {4.729024444062355, 3},
      {-4.72902444452141, 2}, {4.729024444062355, 1}}

I want to get the following information:

  1. all the $2\times1$ sub-lists with the same first element should be together (e.g., {x,y} and {x,z} should be together)
  2. multiplicity of the first elements

I am using Tally and Sort (also used GatherBy) but not much help so far. Thanks in advance.

share|improve this question
add comment

migrated from math.stackexchange.com Sep 12 '12 at 3:11

This question came from our site for people studying math at any level and professionals in related fields.

2 Answers

GatherBy[cc, #[[1]] &]
Length /@ GatherBy[cc, #[[1]] &]
share|improve this answer
7  
GatherBy[cc, First] might be easier to read. –  Sasha Sep 11 '12 at 23:16
    
@Sasha True, but also less general; I applaud belisarius for using Part, though mentioning First is appreciated. –  Mr.Wizard Sep 12 '12 at 10:40
1  
@Mr.Wizard hehe Glad you remember ... now I already was criticized both ways :) –  belisarius Sep 12 '12 at 17:19
add comment

A solution using Gather:

{#, Length /@ #}& @ Gather[cc, First@#1 === First@#2 &]

{{{{0.164411, 1}, {0.164411, 2}}, {{-0.164411, 4}}, {{0.346932, 3}}, {{-0.346932, 2}}, {{-0.261268, 3}}, {{0.261268, 2}}, {{-0.289699, 3}}, {{0.289699, 2}}, {{-7.96869, 3}}, {{7.96869, 2}}, {{-4.00009, 3}}, {{4.00009, 2}}, {{4.72902, 3}, {4.72902, 1}}, {{-4.72902, 2}}}, {2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1}}

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.