dictionary lookup table?

John Mudd mudd at vex.net
Sun Nov 9 12:32:38 EST 2003


I must be missing something here.  It's clearly faster to lookup an item
directly in a dictionary than to scan through a list.  So when I have a
large lookup table I always load it in the form of a dictionary.  But it
seems a waste.  I end up having to assign an artificial value to the
dictionary entry.  Below I assign the value "None" to each dictionary
entry.  

Is there a better way?  I feel like I'm misusing the dictionary. 



#! /usr/bin/env python
                                                                                import random
import time
                                                                        
dict = {}
list = []

n = 10000
                                                                                n = 10000                                                                               
for i in range(n):
    str = '%s' % random.random()
    list.append(str)
    dict[str] = None    # <-- seems a waste
                                                                                
sortedList = list[:]
sortedList.sort()
                                                                                
t1 = time.time()
for i in range(n):
    if sortedList[i] not in list:
        print 'not found'
print 'List lookups: %.3f sec' % (time.time() - t1)
                                                                                
t1 = time.time()
for i in range(n):
    if not dict.has_key(sortedList[i]):
        print 'not found'
print 'List lookups: %.3f sec' % (time.time() - t1)
~
~
~
~
~
:!lookup.py
List lookups: 28.848 sec
List lookups: 0.041 sec
 
Hit ENTER or type command to continue








More information about the Python-list mailing list