Find an Item in a Sorted List

HankC hankc at nospam.com
Tue Feb 26 18:50:58 EST 2002


Greetings:

I'm 2 days new to Python and hope I can get some pointers here...

I have a sorted, delimited text file such as:

Arty,1000
Bobby,2000
Charlie,3000

Knowing a name, I want to return the entire line from the file.  My
approach/problems (any comments appreciated):

# use the filter method
# filter fxn
def Match(line): return line[:toklen] == tok

# find match fxn
def FindMatch:
	fp = open(fname)
	flist = fp.readlines()
	dataline = filter(Match, eodflist)

My problem is that I don't understand how, if it's even possible, to
set tok and toklen values in the FindMatch fxn and pass them to Match.
Is it possible to pass multiple values to the function used by
filter()?

More general...  does filter() have any performance advantage over
just using a for loop?  The datafile list has 10,000+ items.  My
background is in object pascal where I would read the items into a
list then use a binary search to find the target.  I'm hoping there's
an efficient python method to perform a search easily on a sorted
list.

Suppose I want to make tok and toklen global to the module.  Is this
possible?

TIA!



More information about the Python-list mailing list