seaching a list...

Larry Bates larry.bates at websafe.com
Thu Aug 10 19:27:30 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
> 
> furher in the test app, i'm going to have a list, foo:
>  foo = 'a','b','c','d'
> 
> 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...
> 
> also, in searching through google, i haven't come across the list.search
> function.. so just how do i search a list to see if it contains a sublist...
> 
> my real problem involves figuring out how to reduce the number of hits to
> the db/tbl...
> 
> thanks
> 
> ps. if this is confusing, i could provide psuedo-code to make it easier to
> see...
> 
> 
> 
> 
You should use the database for what it is good at storing and searching
through data.  Don't read all the data from a table and search through it.
Rather, create indexes on the table so that you can locate the data quickly
in the database by passing in something you are looking for and let the
database do the searching.  I can promise you this will almost always be
faster and more flexible.  Something like:

Assume the columns are called rownumber, c1, c2, c3, c4 and the table is
indexed on c1, c2, c3, and c4.  This will happen almost instantly no matter
how many rows you are searching for.

select rownumber from database_table
       where c1="a" and c2="b" and c3="c" and c5="d"

It takes one "hit" to the db/table to return the rowindex that matches.

-Larry Bates



More information about the Python-list mailing list