Learning Python via a little word frequency program

MRAB google at mrabarnett.plus.com
Wed Jan 9 18:45:59 EST 2008


On Jan 9, 12:19 pm, Bruno Desthuilliers <bruno.
42.desthuilli... at wtf.websiteburo.oops.com> 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, BTW):
>
> >   Given a string containing a space-separated list of names:
>
> >     names = "freddy fred bill jock kevin andrew kevin kevin jock"
>
> >   produce a frequency table of names, sorted descending by frequency.
> >   then ascending by name. For the above data, the output should be:
>
> >     kevin     : 3
> >     jock      : 2
> >     andrew    : 1
> >     bill      : 1
> >     fred      : 1
> >     freddy    : 1
>
> > 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" % (k, -v)
>
> > I'm interested to learn how more experienced Python folks would solve
> > this little problem.
>
> For a one-shot Q&D script:
>
> names = "freddy fred bill jock kevin andrew kevin kevin jock"
> freqs = [(- names.count(name), name) for name in set(names.split())]
> print "\n".join("%-10s : %s" % (n, -f) for f, n in sorted(freqs))
>
[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".

names = "freddy fred bill jock kevin andrew kevin kevin jock"
name_list = names.split()
freqs = [(- name_list.count(name), name) for name in set(name_list)]
print "\n".join("%-10s : %s" % (n, -f) for f, n in sorted(freqs))



More information about the Python-list mailing list