I have a list of points in 3D and I need to count how many times each point is repeated. For a similar problem with a list of characters many different solutions have been proposed in this post: solutions. Among those, I have here select this solution which is faster:
stringTally = Last @ Reap[Sow[1, #], _, {#, Tr@#2} &] &;
(* to be used as in this example to catch 4 repetitions *)
(* Cases[ stringTally @ list, {x_, 4}:>x] *)
Unfortunately I cannot understand how Reap works in stringTally. I tried to decompose it but the use of two pure functions make the expression a little bit complicated.
Traceon your list of character example (e.g.Cases[stringTally@list, {x_, 4} :> x]and at the same time carefully read the documentation on a separate window you should see how it works. – gwr Sep 30 '15 at 18:40stringTallya bit differently:stringTally = Last@Reap[Sow[1, ##], _, {#1, Tr@#2} &] &. So the##will take the list as it is (with each element being a tag forSowand the#1,#2in the pure function at the end work on each collection as part or theReap.Tr@#2will simply give the total for a vector and each collection will have a number of 1s for each instance as its second part. Hope that helps -- sometimes Mma is very compact and fast but not quite readable for humans... – gwr Sep 30 '15 at 18:47stringTally) so#vs##makes no difference. – george2079 Sep 30 '15 at 19:20