Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2002
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: How Do You Reduce Multiple Elements in a List?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg33878] RE: [mg33863] How Do You Reduce Multiple Elements in a List?
  • From: "Harvey P. Dale" <hpd1 at nyu.edu>
  • Date: Sun, 21 Apr 2002 06:12:27 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

Bob:
	If the list is sorted, Split will do most of the work for you.
(Look it up in the book.)  For example:
lstone={1,1,1,1,2,2,2,3,3,3,3,3,3,4,4,5,5,5,6};
In[1]:= {#[[1]],Length[#]}&/@Split[lstone]
Out[1]= {{1,4},{2,3},{3,6},{4,2},{5,3},{6,1}}
	If the list is not sorted, Split still does most of the work but you
should Sort the list first.  For example:
lsttwo={1,4,3,2,2,3,4,2,5,4,3,6,5,4,3,2,3,4,3,2,1,5};
In[2]:= {#[[1]],Length[#]}&/@Split[Sort[lsttwo]]
Out[2]= {{1,2},{2,5},{3,6},{4,5},{5,3},{6,1}}
	Hope that helps.
	Best,
	Harvey
Harvey P. Dale
University Professor of Philanthropy and the Law
Director, National Center on Philanthropy and the Law
New York University School of Law
Room 206A
110 West 3rd Street
New York, N.Y. 10012-1074
tel: 212-998-6161
fax: 212-995-3149

-----Original Message-----
From: Bob Harris [mailto:nitlion at mindspring.com] 
To: mathgroup at smc.vnet.net
Subject: [mg33878] [mg33863] How Do You Reduce Multiple Elements in a List?

Howdy,

This seems like such a simple thing, yet I've search through the big book
trying to figure it out.  Perhaps someone can help me...

Given a list of integers that generally contains the same number many times,
I'd like to reduce it to a list that just gives me the number/count pairs.
For example, I'd like to convert the list
   {1, 1, 1, 1, 1, 2, 2, 2, 4, 4, 4, 4, 4, 4}
into
   {{1,5}, {2,3}, (4,6}}

Is there a built-in function to do this?  Seems like a histogramming
operation, but I get no useful matches from ?*Hist*.

Thanks,
Bob H


________________________________________________________________________
This email has been scanned for all viruses by the MessageLabs SkyScan
service. For more information on a proactive anti-virus service working
around the clock, around the globe, visit http://www.messagelabs.com
________________________________________________________________________


  • Prev by Date: Best possible help to the developers
  • Next by Date: RE: Need a algorithm
  • Previous by thread: Re: How Do You Reduce Multiple Elements in a List?
  • Next by thread: Drawing a Glass Cylinder?