Re: tallying occurrences in list

2010-06-05 Thread Paul Rubin
Sreenivas Reddy Thatiparthy writes: > How about this one liner, if you prefer them; > set([(k,yourList.count(k)) for k in yourList]) That has a rather bad efficiency problem if the list is large. -- http://mail.python.org/mailman/listinfo/python-list

Re: tallying occurrences in list

2010-06-05 Thread Sreenivas Reddy Thatiparthy
On Jun 4, 11:14 am, kj wrote: > Task: given a list, produce a tally of all the distinct items in > the list (for some suitable notion of "distinct"). > > Example: if the list is ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', > 'c', 'a'], then the desired tally would look something like this: > > [('a',

Re: tallying occurrences in list

2010-06-04 Thread kj
Thank you all! ~K -- http://mail.python.org/mailman/listinfo/python-list

Re: tallying occurrences in list

2010-06-04 Thread Lie Ryan
On 06/05/10 04:38, Magdoll wrote: > On Jun 4, 11:33 am, Peter Otten <__pete...@web.de> wrote: >> kj wrote: >> >>> Task: given a list, produce a tally of all the distinct items in >>> the list (for some suitable notion of "distinct"). >> >>> Example: if the list is ['a', 'b', 'c', 'a', 'b', 'c', 'a'

Re: tallying occurrences in list

2010-06-04 Thread MRAB
kj wrote: Task: given a list, produce a tally of all the distinct items in the list (for some suitable notion of "distinct"). Example: if the list is ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a'], then the desired tally would look something like this: [('a', 4), ('b', 3), ('c', 3)] I

Re: tallying occurrences in list

2010-06-04 Thread Magdoll
On Jun 4, 11:33 am, Peter Otten <__pete...@web.de> wrote: > kj wrote: > > > Task: given a list, produce a tally of all the distinct items in > > the list (for some suitable notion of "distinct"). > > > Example: if the list is ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', > > 'c', 'a'], then the desired

Re: tallying occurrences in list

2010-06-04 Thread Magdoll
On Jun 4, 11:28 am, Paul Rubin wrote: > kj writes: > > 1. is there a standard name for it? > > I don't know of one, or a stdlib for it, but it's pretty trivial. > > > def tally(c): > >     t = dict() > >     for x in c: > >         t[x] = t.get(x, 0) + 1 > >     return sorted(t.items(), key=lambd

Re: tallying occurrences in list

2010-06-04 Thread Peter Otten
kj wrote: > > > > > > Task: given a list, produce a tally of all the distinct items in > the list (for some suitable notion of "distinct"). > > Example: if the list is ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', > 'c', 'a'], then the desired tally would look something like this: > > [('a', 4),

Re: tallying occurrences in list

2010-06-04 Thread Paul Rubin
kj writes: > 1. is there a standard name for it? I don't know of one, or a stdlib for it, but it's pretty trivial. > def tally(c): > t = dict() > for x in c: > t[x] = t.get(x, 0) + 1 > return sorted(t.items(), key=lambda x: (-x[1], x[0])) I like to use defaultdict and tuple

tallying occurrences in list

2010-06-04 Thread kj
Task: given a list, produce a tally of all the distinct items in the list (for some suitable notion of "distinct"). Example: if the list is ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a'], then the desired tally would look something like this: [('a', 4), ('b', 3), ('c', 3)] I find myself