Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've got a list in the format of list={{a,1},{a,1},{b,1},{c,1},{b,1}} And I want it to "compress" into a list of pairs {element, frequency} such as this: {{a,2},{b,2},{c,1}}

I've come up with this (faulty) solution:

upravit[l_List] := {l[[1]], l[[2]] + 1}
compression[list_List] := 
    list //. {a___, b_List, c_List, d___} /; c[[1]] == b[[1]] -> {a, upravit[b], d}

The problem is that I'm only checking elements that are next to each other, and so in the example, I'd end up with{{a,2},{b,1},{c,1},{b,1}}

Therefore, either I need to correct my code, or come up with an entirely different method (using similar methods thought, as this is a homework).

Thanks for any help!

Edit: as pointed out in the comments and answers, there is a function that easily does that. I should've specified that this should be done using the methods in my example only. Sorry!

share|improve this question

marked as duplicate by Mr.Wizard Sep 20 '14 at 10:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

you want to count based on just the letters? Something like: Tally[list[[All, 1]]]? – Pinguin Dirk Jan 28 '13 at 11:30
Yes, that is exactly what I need, only using the methods I used, as using "tally" is against the purpose of the homework (that is to learn these methods) – Dahn Jahn Jan 28 '13 at 11:33
ah ok - so maybe just Sort your list before running compression? (not sure what you are allowed to do and what not) – Pinguin Dirk Jan 28 '13 at 11:36
I do not know why that didn't occur to me. Feel stupid now! Thanks for the help. – Dahn Jahn Jan 28 '13 at 11:40
I don't really have a problem with (suitably tagged) homework questions being posted here, but isn't the point of homework that you figure these kinds of issues out for yourself? I can't quite understand what it accomplishes if someone else gives you the solution. Also, don't be surprised if your tutor is active on this site--there aren't that many venues like this for Mathematica users and a large fraction of the users here are academics. – Oleksandr R. Jan 28 '13 at 12:43
up vote 1 down vote accepted

Asking for help completing homework assignments is a sketchy subject. I'll help with hints, however a full solutions seems to be against the purpose of this site.

As others have mentioned there is a built-in method for this Tally as for your own method, you are correct in that it won't work since it's only matching adjacent objects, you can fix this by allowing elements in between in the pattern something like: {before___,a_List,between___,b_List,after___}:>{someF[a,b],before,between,after}. As for alternative methods, you could use pattern matching to first find out what unique elements exist, and then afterwards for each unique element extract all pairs for that one and sum them, eg: {a,Total@Cases[list,{a,n_}:>n]}. I'll leave it up to you if you want to try this out, perhaps testing to see if it's faster then your other approach.

share|improve this answer
I agree with your first paragraph :) Thanks for the tips! – Dahn Jahn Jan 28 '13 at 11:45
Good 'homework answer'! From what I read, your 'seems to be' is more of a 'is definitely'. – cormullion Jan 28 '13 at 14:58

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