Returning histogram-like data for items in a list
George Sakkis
gsakkis at rutgers.edu
Thu Jul 21 20:19:34 EDT 2005
"Michael Hoffman" <cam.ac.uk at mh391.invalid> wrote:
> Ric Deez wrote:
> > Hi there,
> >
> > I have a list:
> > L1 = [1,1,1,2,2,3]
> >
> > How can I easily turn this into a list of tuples where the first element
> > is the list element and the second is the number of times it occurs in
> > the list (I think that this is referred to as a histogram):
> >
> > i.e.:
> >
> > L2 = [(1,3),(2,2),(3,1)]
>
> >>> import itertools
> >>> L1 = [1,1,1,2,2,3]
> >>> L2 = [(key, len(list(group))) for key, group in itertools.groupby(L1)]
> >>> L2
> [(1, 3), (2, 2), (3, 1)]
> --
> Michael Hoffman
This is correct if the original list items are grouped together; to be on the safe side, sort it
first:
L2 = [(key, len(list(group))) for key, group in itertools.groupby(sorted(L1))]
Or if you care about performance rather than number of lines, use this:
def hist(seq):
h = {}
for i in seq:
try: h[i] += 1
except KeyError: h[i] = 1
return h.items()
George
More information about the Python-list
mailing list