Most efficient solution?

Christopher A. Craig com-nospam at ccraig.org
Fri Jul 20 16:50:30 EDT 2001


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

phawkins at connact.com writes:

> def dict(a,c):
> 	start = time.clock()
> 	for item in a:
> 		d = filter(c.has_key, a)

this is an error.  You are creating a list containing items in 'a' and
'c' for each item in a.  You only need to make the list once.

with the correction dict runs in .04 seconds while comp runs in 58.09

I would say that you still need to use dictionaries (note that this
doesn't actually solve the problem, which is to get items in 'a' but
not in 'b', but the following

def dict2(a, c):
   start = time.time()
   d = [ i for i in a if not c.has_key(i)]
   end = time.time()
   print `end-start`

does in in .14 seconds (still 400x faster than comp)

- -- 
Christopher A. Craig <com-nospam at ccraig.org>
"The absolute value of the TA was less than epsilon" - MIT Course Evaluation
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Processed by Mailcrypt

iEYEARECAAYFAjtYmZYACgkQjVztv3T8pzsxhQCgn9cZvI72NqkldzBKVx+azg7d
XC4AnRBQU4Zbze69paircJb8Rm8ZVjiN
=EUxR
-----END PGP SIGNATURE-----




More information about the Python-list mailing list