[Tutor] Location of found item in list.

Chris Hengge pyro9219 at gmail.com
Thu Oct 19 04:17:18 CEST 2006


I think this is overkill for what I'm trying to do, could you please refer
to what I just wrote luke? That might make things simpler..

On 10/18/06, John Fouhy <john at fouhy.net> wrote:
>
> 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.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20061018/77c7652a/attachment.htm 


More information about the Tutor mailing list