[Tutor] Word count help

Kalle Svensson kalle@gnupung.net
Tue, 30 Jan 2001 21:18:44 +0100


--9crTWz/Z+Zyzu20v
Content-Type: text/plain; charset=iso-8859-1
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Hi.  Just a minor note...

Sez Jose Amoreira:
> #----------------------------
> def Wordcount(testo,freqs):
>     # freqs is a dictionary with words as keys and their frequencies as
>     # values; testo is a part of the input file(I'm trying to keep your c=
ode
>     # structure, but removed the OO because I didn't understand it
>     for word in test.split():
>         if word in freqs.keys():

Shouldn't this be
          if freqs.has_key(word):
?

IIRC, dict.keys() and if x in list are both linear time.  dict.has_key() is
constant time.  I repeat, IIRC. <wink>  And somebody might have optimized i=
t.

Peace,
  Kalle
--=20
Email: kalle@gnupung.net     | You can tune a filesystem, but you
Web: http://www.gnupung.net/ | can't tune a fish. -- man tunefs(8)
PGP fingerprint: 0C56 B171 8159 327F 1824 F5DE 74D7 80D7 BF3B B1DD

--9crTWz/Z+Zyzu20v
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.4 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE6dyGkdNeA1787sd0RAtTrAJ0VkcNebp1jbtyJGOBMflglzMYZRwCffx55
lYOimo1Xvzk9Ilmc/6MSOWQ=
=T9uM
-----END PGP SIGNATURE-----

--9crTWz/Z+Zyzu20v--