seaching a list...

Simon Forman rogue_pedro at yahoo.com
Thu Aug 10 15:56:56 EDT 2006


bruce wrote:
> hi...
>
> i'm playing with a test sample. i have somethhing like:
>  dog = mysql_get(.....)
>  .
>  .
>  .
>
> such that 'dog' will be an 'AxB' array of data from the tbls

What's an "'AxB' array", do you mean a list of lists?  If not, what
kind of object do you mean and what methods does it have?

>
> furher in the test app, i'm going to have a list, foo:
>  foo = 'a','b','c','d'

That's a tuple, not a list.

>
> i'm trying to determine what's the fastest way of searching through the
> 'dog' array/list of information for the 'foo' list.
>
> should i essentially make dog into A lists, where each list is B elements,
> or should it somehow combine all the elements/items in 'dog' into one large
> list, and then search through that for the 'foo' list...

This really depends on what kind of searching you want to do, and what
kind of results you want.

If dog is a list of lists, and foo might occur as one of the sublists,
then you could do something like this:

try:
    i = dog.index(foo)
except ValueError:
    i = -1

>
> also, in searching through google, i haven't come across the list.search
> function..

That's because it doesn't exist.

> so just how do i search a list to see if it contains a sublist...

One thing that can do this is the difflib.SequenceMatcher() class.
Read this: http://docs.python.org/lib/sequence-matcher.html

|>> from difflib import SequenceMatcher
|>> N = range(10)
|>> M = range(3, 6)
|>> s = SequenceMatcher(a=N, b=M)
|>> c = len(M)
|>> [i for i, j, n in s.get_matching_blocks() if n == c]
[3]


There may be better ways.

>
> my real problem involves figuring out how to reduce the number of hits to
> the db/tbl...

What?

>
> thanks
>
> ps. if this is confusing, i could provide psuedo-code to make it easier to
> see...

Yes, please.


Peace,
~Simon




More information about the Python-list mailing list