Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Returning histogram-like data for items in a list

Reply
Thread Tools

Returning histogram-like data for items in a list

 
 
Ric Deez
Guest
Posts: n/a
 
      07-21-2005
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)]

I was doing something like:

myDict = {}
for i in L1:
myDict.setdefault(i,[]).append(i)

then doing this:

L2 = []
for k, v in myDict.iteritems():
L2.append((k, len(v)))

This works but I sort of feel like there ought to be an easier way,
rather than to have to store the list elements, when all I want is a
count of them. Would anyone care to comment?

I also tried this trick, where locals()['_[1]'] refers to the list
comprehension itself as it gets built, but it gave me unexpected results:

>>> L2 = [(i, len(i)) for i in L2 if not i in locals()['_[1]']]
>>> L2

[((1, 3), 2), ((2, 2), 2), ((3, 1), 2)]

i.e. I don't understand why each tuple is being counted as well.

Regards,

Ric
 
Reply With Quote
 
 
 
 
Michael Hoffman
Guest
Posts: n/a
 
      07-21-2005
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
 
Reply With Quote
 
 
 
 
George Sakkis
Guest
Posts: n/a
 
      07-22-2005
"Michael Hoffman" <(E-Mail Removed)> 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


 
Reply With Quote
 
jeethu_rao
Guest
Posts: n/a
 
      07-22-2005
Adding to George's reply, if you want slightly more performance, you
can avoid the exception with something like

def hist(seq):
h = {}
for i in seq:
h[i] = h.get(i,0)+1
return h.items()

Jeethu Rao

 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      07-22-2005
Ric Deez a écrit :
> 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)]
>
> I was doing something like:
>
> myDict = {}
> for i in L1:
> myDict.setdefault(i,[]).append(i)
>
> then doing this:
>
> L2 = []
> for k, v in myDict.iteritems():
> L2.append((k, len(v)))
>
> This works but I sort of feel like there ought to be an easier way,


If you don't care about order (but your solution isn't garanteed to
preserve order either...):

L2 = dict([(item, L1.count(item)) for item in L1]).items()

But this may be inefficient is the list is large, so...

def hist(seq):
d = {}
for item in seq:
if not item in d:
d[item] = seq.count(item)
return d.items()

> I also tried this trick, where locals()['_[1]'] refers to the list


Not sure to understand how that one works... But anyway, please avoid
this kind of horror unless your engaged in WORN context with a
perl-monger !-).
 
Reply With Quote
 
George Sakkis
Guest
Posts: n/a
 
      07-22-2005
"jeethu_rao" <(E-Mail Removed)> wrote:

> Adding to George's reply, if you want slightly more performance, you
> can avoid the exception with something like
>
> def hist(seq):
> h = {}
> for i in seq:
> h[i] = h.get(i,0)+1
> return h.items()
>
> Jeethu Rao


The performance penalty of the exception is imposed only the first time a distinct item is found. So
unless you have a huge list of distinct items, I seriously doubt that this is faster at any
measurable rate.

George


 
Reply With Quote
 
David Isaac
Guest
Posts: n/a
 
      07-22-2005

"Ric Deez" <(E-Mail Removed)> wrote in message
news:dbpat7$28o$(E-Mail Removed)...
> 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):


For ease of reading (but not efficiency) I like:
hist = [(x,L1.count(x)) for x in set(L1)]
See http://aspn.activestate.com/ASPN/Coo.../Recipe/277600

Alan Isaac


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What's your preferred way of returning a list of items? DeMarcus C++ 26 05-15-2010 09:16 PM
unable to retrieve listbox items on postback , items moved usingjavascript between 2 list boxes (source and target ) divya ASP .Net 1 05-28-2008 05:27 AM
Is there any way to append some items to List box, without retrieving all items through AJAX? Anjan Bhowmik ASP .Net 1 02-14-2008 09:02 PM
returning none when it should be returning a list? randomtalk@gmail.com Python 11 05-02-2006 10:26 AM
grouping items among a list according to items subtag value Gilles Kuhn XML 0 09-15-2003 12:01 PM



Advertisments