Re: Learning Python via a little word frequency program

2008-01-11 Thread rent
import collections names = freddy fred bill jock kevin andrew kevin kevin jock freq = collections.defaultdict(int) for name in names.split(): freq[name] += 1 keys = freq.keys() keys.sort(key = freq.get, reverse = True) for k in keys: print %-10s: %d % (k, freq[k]) On Jan 9, 6:58 pm,

Re: Learning Python via a little word frequency program

2008-01-11 Thread Paul Rubin
rent [EMAIL PROTECTED] writes: keys = freq.keys() keys.sort(key = freq.get, reverse = True) for k in keys: print %-10s: %d % (k, freq[k]) I prefer (untested): def snd((x,y)): return y # I wish this was built-in sorted_freq = sorted(freq.iteritems(), key=snd, reverse=True) for

Re: Learning Python via a little word frequency program

2008-01-11 Thread Mike Meyer
On 11 Jan 2008 03:50:53 -0800 Paul Rubin http://phr.cx@NOSPAM.invalid wrote: rent [EMAIL PROTECTED] writes: keys = freq.keys() keys.sort(key = freq.get, reverse = True) for k in keys: print %-10s: %d % (k, freq[k]) I prefer (untested): def snd((x,y)): return y # I wish

Re: Learning Python via a little word frequency program

2008-01-11 Thread Hrvoje Niksic
Mike Meyer [EMAIL PROTECTED] writes: On 11 Jan 2008 03:50:53 -0800 Paul Rubin http://phr.cx@NOSPAM.invalid wrote: rent [EMAIL PROTECTED] writes: keys = freq.keys() keys.sort(key = freq.get, reverse = True) for k in keys: print %-10s: %d % (k, freq[k]) I prefer (untested):

Re: Learning Python via a little word frequency program

2008-01-10 Thread Bruno Desthuilliers
MRAB a écrit : On Jan 9, 12:19 pm, Bruno Desthuilliers bruno. [EMAIL PROTECTED] wrote: (snip) That actually prints: kevin : 3 fred : 2 jock : 2 andrew : 1 bill : 1 freddy : 1 It says that fred occurs twice because of freddy. oops ! My bad, didn't spot

Re: Learning Python via a little word frequency program

2008-01-10 Thread Bruno Desthuilliers
Paul Hankin a écrit : On Jan 9, 12:19 pm, Bruno Desthuilliers bruno. [EMAIL PROTECTED] wrote: Andrew Savige a écrit : and the -x hack above to achieve a descending sort feels a bit odd to me, though I couldn't think of a better way to do it. The other way would be to pass a custom

Learning Python via a little word frequency program

2008-01-09 Thread Andrew Savige
I'm learning Python by reading David Beazley's Python Essential Reference book and writing a few toy programs. To get a feel for hashes and sorting, I set myself this little problem today (not homework, BTW): Given a string containing a space-separated list of names: names = freddy fred

Re: Learning Python via a little word frequency program

2008-01-09 Thread Fredrik Lundh
Andrew Savige wrote: Here's my first attempt: names = freddy fred bill jock kevin andrew kevin kevin jock freq = {} for name in names.split(): freq[name] = 1 + freq.get(name, 0) deco = zip([-x for x in freq.values()], freq.keys()) deco.sort() for v, k in deco: print %-10s: %d

Re: Learning Python via a little word frequency program

2008-01-09 Thread Ant
I'm interested to learn how more experienced Python folks would solve this little problem. I think I'd do the following: from collections import defaultdict names = freddy fred bill jock kevin andrew kevin kevin jock freq = defaultdict(lambda: 0) for name in names.split(): freq[name] +=

Re: Learning Python via a little word frequency program

2008-01-09 Thread Peter Otten
Andrew Savige wrote: I'm learning Python by reading David Beazley's Python Essential Reference book and writing a few toy programs. To get a feel for hashes and sorting, I set myself this little problem today (not homework, BTW): Given a string containing a space-separated list of names:

Re: Learning Python via a little word frequency program

2008-01-09 Thread Andrew Savige
Fredrik Lundh wrote: # sort items on descending count deco = sorted(freq.items(), key=lambda x: -x[1]) Neat. Is there any way to use sorted() with multiple sort keys? ... Given that the spec calls for sorting by _two_ keys: first by frequency (descending), then by name (ascending). To

Re: Learning Python via a little word frequency program

2008-01-09 Thread Fredrik Lundh
Andrew Savige wrote: Neat. Is there any way to use sorted() with multiple sort keys? ... sure! just return the composite key as a tuple: # sort on descending count, ascending name deco = sorted(freq.items(), key=lambda x: (-x[1], x[0])) (when comparing tuples, Python first compares

Re: Learning Python via a little word frequency program

2008-01-09 Thread Bruno Desthuilliers
Andrew Savige a écrit : I'm learning Python by reading David Beazley's Python Essential Reference book and writing a few toy programs. To get a feel for hashes and sorting, I set myself this little problem today (not homework, BTW): Given a string containing a space-separated list of

Re: Learning Python via a little word frequency program

2008-01-09 Thread Peter Otten
Andrew Savige wrote: Fredrik Lundh wrote: # sort items on descending count deco = sorted(freq.items(), key=lambda x: -x[1]) Neat. Is there any way to use sorted() with multiple sort keys? ... Given that the spec calls for sorting by _two_ keys: first by frequency (descending),

Re: Learning Python via a little word frequency program

2008-01-09 Thread Bruno Desthuilliers
Ant a écrit : I'm interested to learn how more experienced Python folks would solve this little problem. I think I'd do the following: from collections import defaultdict names = freddy fred bill jock kevin andrew kevin kevin jock freq = defaultdict(lambda: 0) for name in

Re: Learning Python via a little word frequency program

2008-01-09 Thread MRAB
On Jan 9, 12:19 pm, Bruno Desthuilliers bruno. [EMAIL PROTECTED] wrote: Andrew Savige a écrit : I'm learning Python by reading David Beazley's Python Essential Reference book and writing a few toy programs. To get a feel for hashes and sorting, I set myself this little problem today (not

Re: Learning Python via a little word frequency program

2008-01-09 Thread Paul Hankin
On Jan 9, 12:19 pm, Bruno Desthuilliers bruno. [EMAIL PROTECTED] wrote: Andrew Savige a écrit : and the -x hack above to achieve a descending sort feels a bit odd to me, though I couldn't think of a better way to do it. The other way would be to pass a custom comparison callback to sort,