Re: Counting number of each item in a list.

2006-03-20 Thread Peter Otten
per9000 wrote: > A good idea could be to change the header of the first one (if you want > the option to start counting from zero) into: > def dict_add(inlist,indict={},init=1): make that def dict_add(inlist, indict=None, init=1): if indict is None: indict = {} See "5. Mutable defau

Re: Counting number of each item in a list.

2006-03-20 Thread per9000
Dear counters, I have also encountered the above problem, and I frequently use the two blocks of code below. The first one is commented already - except that this version does not start from an empty dict. The second "inverts" the first one. Meaning that a dict in word2hits style: my_dict['the'

Re: Counting number of each item in a list.

2006-03-19 Thread Bruno Desthuilliers
Justin Azoff a écrit : > Bruno Desthuilliers wrote: > >> My solution seems to be faster than Paul's >>one (but slower than bearophile's), be it on small, medium or large lists. > > > Your version is only fast on lists with a very small number of unique > elements. > > changing mklist to have >

Re: Counting number of each item in a list.

2006-03-19 Thread Bruno Desthuilliers
Justin Azoff a écrit : > Bruno Desthuilliers wrote: > >>And of course, I was right. My solution seems to be faster than Paul's >>one (but slower than bearophile's), be it on small, medium or large lists. > > > Your version is only fast on lists with a very small number of unique > elements. > >

Re: Counting number of each item in a list.

2006-03-19 Thread Bruno Desthuilliers
Paul Rubin a écrit : > Bruno Desthuilliers <[EMAIL PROTECTED]> writes: > >>And of course, I was right. My solution seems to be faster than Paul's >>one (but slower than bearophile's), be it on small, medium or large >>lists. >> >>nb: A is mine, B is Paul's and C is bearophile's, and the number aft

Re: Counting number of each item in a list.

2006-03-19 Thread Justin Azoff
Bruno Desthuilliers wrote: > And of course, I was right. My solution seems to be faster than Paul's > one (but slower than bearophile's), be it on small, medium or large lists. Your version is only fast on lists with a very small number of unique elements. changing mklist to have items = range(64

Re: Counting number of each item in a list.

2006-03-19 Thread Paul Rubin
Bruno Desthuilliers <[EMAIL PROTECTED]> writes: > And of course, I was right. My solution seems to be faster than Paul's > one (but slower than bearophile's), be it on small, medium or large > lists. > > nb: A is mine, B is Paul's and C is bearophile's, and the number after > is the size of the li

Re: Counting number of each item in a list.

2006-03-19 Thread Bruno Desthuilliers
Bruno Desthuilliers a écrit : > Kent Johnson a écrit : > >> sophie_newbie wrote: >> >>> Hey Bruno, >>> >>> I got an invalid syntax error when i tried using your "str_counts = >>> dict((s, str_list.count(s) for s in set(str_list))" bit of code? Maybe >>> there is a missing bracket or comma? Or mayb

Re: Counting number of each item in a list.

2006-03-19 Thread Bruno Desthuilliers
Kent Johnson a écrit : > sophie_newbie wrote: > >> Hey Bruno, >> >> I got an invalid syntax error when i tried using your "str_counts = >> dict((s, str_list.count(s) for s in set(str_list))" bit of code? Maybe >> there is a missing bracket or comma? Or maybe I need to import >> something. > > >

Re: Counting number of each item in a list.

2006-03-19 Thread bearophileHUGS
With CPython this is probably the faster version, the version with list.count() has to be used only if you are sure to always have a short input list, because it's O(n^2): str_list = ['StringA', 'StringC', 'StringB', 'StringA', 'StringC', 'StringD', 'StringA' ] str_count

Re: Counting number of each item in a list.

2006-03-19 Thread Bruno Desthuilliers
sophie_newbie a écrit : > Hey Bruno, > > I got an invalid syntax error when i tried using your "str_counts = > dict((s, str_list.count(s) for s in set(str_list))" bit of code? Maybe > there is a missing bracket or comma? Yes, sorry, see Kent's post for the correction. > Or maybe I need to impor

Re: Counting number of each item in a list.

2006-03-19 Thread Kent Johnson
sophie_newbie wrote: > Hey Bruno, > > I got an invalid syntax error when i tried using your "str_counts = > dict((s, str_list.count(s) for s in set(str_list))" bit of code? Maybe > there is a missing bracket or comma? Or maybe I need to import > something. It should be str_counts = dict((s, str_l

Re: Counting number of each item in a list.

2006-03-19 Thread sophie_newbie
Hi Paul, Ur bit of code works, thanks so much, just for anyone reading this use 'h = {}' instead of h = 0. Thanks -- http://mail.python.org/mailman/listinfo/python-list

Re: Counting number of each item in a list.

2006-03-19 Thread sophie_newbie
Hey Bruno, I got an invalid syntax error when i tried using your "str_counts = dict((s, str_list.count(s) for s in set(str_list))" bit of code? Maybe there is a missing bracket or comma? Or maybe I need to import something. Thanks so much for your help. -- http://mail.python.org/mailman/listinf

Re: Counting number of each item in a list.

2006-03-19 Thread Bruno Desthuilliers
sophie_newbie a écrit : > I have a list a little something like this: > > StringA > StringC > StringB > StringA > StringC > StringD > StringA > ... > etc. > > Basically I was wondering if there was an easy way to return how many > of each string are in the list, something like this: > > StringA

Re: Counting number of each item in a list.

2006-03-19 Thread Paul Rubin
"sophie_newbie" <[EMAIL PROTECTED]> writes: > I suppose that the easiest way to do that is to convert it to a 2 > dimensional array? Is there any easy way? Use a hash table. Untested: h = 0 for x in your_list: h[x] = h.get(x, 0) + 1 You can then use something like sorted(h.items())

Counting number of each item in a list.

2006-03-19 Thread sophie_newbie
I have a list a little something like this: StringA StringC StringB StringA StringC StringD StringA ... etc. Basically I was wondering if there was an easy way to return how many of each string are in the list, something like this: StringA - 3 StringB - 1 StringC - 2 StringD - 1 I suppose that