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])
>
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)): ret
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)
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 p
Paul Hankin a écrit :
> On Jan 9, 12:19 pm, Bruno Desthuilliers [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
MRAB a écrit :
> On Jan 9, 12:19 pm, Bruno Desthuilliers [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
On Jan 9, 12:19 pm, Bruno Desthuilliers 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,
> which would
On Jan 9, 12:19 pm, Bruno Desthuilliers 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 homework, BT
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
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 (descend
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:
> 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
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 c
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 n
> 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:
> 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
16 matches
Mail list logo