Re: get method

2008-12-30 Thread James Mills
On Wed, Dec 31, 2008 at 10:54 AM, MRAB wrote: > Occasionally someone posts here wanting to count items and solutions > involving dict or defaultdict are suggested, and I think that a 'bag' class > would be useful. The 'set' class was introduced first in a module, but it > soon became a builtin. My

Re: get method

2008-12-30 Thread James Mills
On Wed, Dec 31, 2008 at 10:49 AM, Steven D'Aprano wrote: > What set module? Sorry I must have meant the collections module :) > Adding a multi-set or bag class to the collections module would be a good > idea though. Perhaps you should put in a feature request? :) Perhaps I will. cheers James

Re: get method

2008-12-30 Thread MRAB
James Mills wrote: On Wed, Dec 31, 2008 at 10:22 AM, John Machin wrote: (snip) The "crawl through the shrubbery looking for evidence" approach stumbles on the actual code: Yes I found his implementation soon after :) Not bad actually... I wonder why bag() isn't shipped with the std lib - per

Re: get method

2008-12-30 Thread Steven D'Aprano
On Wed, 31 Dec 2008 10:29:14 +1000, James Mills wrote: > On Wed, Dec 31, 2008 at 10:22 AM, John Machin > wrote: (snip) > >> The "crawl through the shrubbery looking for evidence" approach >> stumbles on the actual code: > > Yes I found his implementation soon after :) Not bad actually... I > wo

Re: get method

2008-12-30 Thread James Mills
On Wed, Dec 31, 2008 at 10:22 AM, John Machin wrote: (snip) > The "crawl through the shrubbery looking for evidence" approach > stumbles on the actual code: Yes I found his implementation soon after :) Not bad actually... I wonder why bag() isn't shipped with the std lib - perhaps in teh set mod

Re: get method

2008-12-30 Thread John Machin
On Dec 31, 10:58 am, "James Mills" wrote: > On Wed, Dec 31, 2008 at 9:15 AM, MRAB wrote: > > (snip) > > > A while back I posted a Python implementation of 'bag' (also called a > > multiset). The code would then become something like: > > What complexity is this ? The "armchair philosopher" appro

Re: get method

2008-12-30 Thread James Mills
On Wed, Dec 31, 2008 at 9:15 AM, MRAB wrote: (snip) > A while back I posted a Python implementation of 'bag' (also called a > multiset). The code would then become something like: What complexity is this ? cheers James -- http://mail.python.org/mailman/listinfo/python-list

Re: get method

2008-12-30 Thread MRAB
James Mills wrote: On Tue, Dec 30, 2008 at 7:10 PM, Roel Schroeven wrote: Hm, you just changed an O(n) algorithm to an O(n**2) algorithm. No big deal for short strings, but try your solution on a string with length 1 and see the difference. On my computer the O(n) version takes 0.008 second

Re: get method

2008-12-30 Thread James Mills
On Tue, Dec 30, 2008 at 7:10 PM, Roel Schroeven wrote: > Hm, you just changed an O(n) algorithm to an O(n**2) algorithm. No big > deal for short strings, but try your solution on a string with length > 1 and see the difference. On my computer the O(n) version takes > 0.008 seconds, while your

Re: get method

2008-12-30 Thread Roel Schroeven
James Mills schreef: > Ross, the others have informed you that you are not > actually incrementing the count. I'll assume you've > fixed your function now :) ... I want to show you a far > simpler way to do this which takes advantage of > Python's list comprehensions and mappings (which are > reall

Re: get method

2008-12-29 Thread Aaron Brady
On Dec 29, 8:02 pm, Steven D'Aprano wrote: > On Mon, 29 Dec 2008 17:38:36 -0800, Ross wrote: > > On Dec 29, 8:07 pm, Scott David Daniels wrote: > >> Ross wrote: > >> > ... Use get to write histogram more concisely. You should be able to > >> > eliminate the if statement. > > >> > def histogram(s)

Re: get method

2008-12-29 Thread Steven D'Aprano
On Mon, 29 Dec 2008 17:38:36 -0800, Ross wrote: > On Dec 29, 8:07 pm, Scott David Daniels wrote: >> Ross wrote: >> > ... Use get to write histogram more concisely. You should be able to >> > eliminate the if statement. >> >> > def histogram(s): >> >    d = dict() >> >    for c in s: >> >        

Re: get method

2008-12-29 Thread James Mills
On Tue, Dec 30, 2008 at 11:43 AM, James Mills wrote: > On Tue, Dec 30, 2008 at 11:38 AM, Ross wrote: >> I realize the code isn't counting, but how am I to do this without >> using an if statement as the problem instructs? > > I just gave you a hint :) Ross: This exercise is a simple exercise de

Re: get method

2008-12-29 Thread James Mills
On Tue, Dec 30, 2008 at 11:38 AM, Ross wrote: > I realize the code isn't counting, but how am I to do this without > using an if statement as the problem instructs? I just gave you a hint :) cheers James -- http://mail.python.org/mailman/listinfo/python-list

Re: get method

2008-12-29 Thread Ross
On Dec 29, 8:07 pm, Scott David Daniels wrote: > Ross wrote: > > ... Use get to write histogram more concisely. You should be able to > > eliminate the if statement. > > > def histogram(s): > >    d = dict() > >    for c in s: > >            d[c]= d.get(c,0) > >    return d > > > This code returns

Re: get method

2008-12-29 Thread James Mills
On Tue, Dec 30, 2008 at 11:32 AM, James Mills wrote: > Ross, the others have informed you that you are not > actually incrementing the count. I'll assume you've > fixed your function now :) ... I want to show you a far > simpler way to do this which takes advantage of > Python's list comprehension

Re: get method

2008-12-29 Thread James Mills
On Tue, Dec 30, 2008 at 11:00 AM, Ross wrote: > I am teaching myself Python by going through Allen Downing's "Think > Python." I have come across what should be a simple exercise, but I am > not getting the correct answer. Here's the exercise: > > Given: > > def histogram(s): >d = dict() >

Re: get method

2008-12-29 Thread Scott David Daniels
Ross wrote: ... Use get to write histogram more concisely. You should be able to eliminate the if statement. def histogram(s): d = dict() for c in s: d[c]= d.get(c,0) return d This code returns a dictionary of all the letters to any string s I give it but

Re: get method

2008-12-29 Thread Steven D'Aprano
On Mon, 29 Dec 2008 17:00:31 -0800, Ross wrote: > Here's my code: > > def histogram(s): > d = dict() > for c in s: > d[c]= d.get(c,0) > return d > > This code returns a dictionary of all the letters to any string s I give > it but each corresponding value is incor