[Tutor] Location of found item in list.

John Fouhy john at fouhy.net
Thu Oct 19 04:15:18 CEST 2006


Ok, so your question is:

    If nothing in directoryList matches item, how do I find the
element of directoryList that should have matched?

This is a bit tricky, because you need to define exactly what results
you expect.

(for example, suppose limitedLineList contained 'SL_39.sdr' and
directoryList had 'SL39.sdr'.  Is that something that should be a
match?)

Possibly you could do this:

def match(item, lst):
    """ See if item is present in lst.

    output :: bool
    """
    return item in lst

def near_match(item, lst):
    """ See if item is similar to something in lst. Currently, we just
match case-insensitive.

    output :: first matching element of lst, or None
    """
    for item2 in lst:
        if item.upper() == item2.upper():
            return item2
    return None

for item in limitedLineList:
    if match(item, directoryList):
        print '%20s%20s%20s' % ('Match!', item, item)
    else:
        item2 = near_match(item, directoryList)
        if item2:
            print '%20s%20s%20s' % ('Fail!', item, item2)
        else:
            print '%20s%20s%20s' % ('Fail!', item, '????')

You can modify near_match if you have some other definition of what
you want it to return.

-- 
John.


More information about the Tutor mailing list