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,
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
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
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):
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
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
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
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
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] +=
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:
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
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
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
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),
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
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
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,
17 matches
Mail list logo