Re: Extract values and multilpicities from list
- To: mathgroup at smc.vnet.net
- Subject: [mg64862] Re: [mg64849] Extract values and multilpicities from list
- From: "David Park" <djmp at earthlink.net>
- Date: Mon, 6 Mar 2006 05:01:07 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Wolfgang, Here is the obvious solution. ValsAndMultiplicities[list_?VectorQ] := {First[#], Length[#]} & /@ Split@Sort[list] lstIn = {2, 3, 4, 4, 2, 1, 1, 5, 4}; ValsAndMultiplicities[lstIn] {{1, 2}, {2, 2}, {3, 1}, {4, 3}, {5, 1}} testlist = Array[Random[Integer, {0, 9}] &, 100000]; ValsAndMultiplicities[testlist] {{0, 9905}, {1, 9952}, {2, 10064}, {3, 10029}, {4, 9980}, {5, 9992}, {6, 9928}, {7, 10126}, {8, 9931}, {9, 10093}} David Park djmp at earthlink.net http://home.earthlink.net/~djmp/ From: Dr. Wolfgang Hintze [mailto:weh at snafu.de] To: mathgroup at smc.vnet.net Given a list of integers which may repeat, e.g. lstIn = {2,3,4,4,2,1,1,5,4} provide a list of different values and their respective multiplicities, in the example, LstOut= {{1,2},{2,2},{3,1},{4,3},{5,1}} Who finds the shortest function doing this task in general? Thanks. Best regards, Wolfgang