Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > seaching a list...

Reply
Thread Tools

seaching a list...

 
 
bruce
Guest
Posts: n/a
 
      08-10-2006
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...




 
Reply With Quote
 
 
 
 
Simon Forman
Guest
Posts: n/a
 
      08-10-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

 
Reply With Quote
 
 
 
 
Larry Bates
Guest
Posts: n/a
 
      08-10-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
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      08-11-2006

bruce wrote:
> hi larry...
>
> thanks for the reply...
>
> the issue i'm having is that i'm going to have to compare multiple rows of
> information to the information in the db. so essentially i'd have to do a
> hit to the db, for each row of information i want to compare if i did it
> your way... (which was what i had thought about)
>
> the issue of doing the string/list compare/search is that i can get
> everything from the db with one call... i can then iterate through memory
> for each of my row information that i'm searching to see if it exists in the
> db...
>
> memory searches should be faster than the network overhead, and the
> associated multiple db calls...


(1) Don't top-post.
(2) Do as asked: supply some pseudo-code for comparing a row of query
information with a row extracted from your database -- whether you are
comparing on one/some/all columns and what type of comparison are vital
pieces of information. How many query rows? How many database rows?

 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      08-11-2006
At Thursday 10/8/2006 21:54, bruce wrote:

>the issue i'm having is that i'm going to have to compare multiple rows of
>information to the information in the db. so essentially i'd have to do a
>hit to the db, for each row of information i want to compare if i did it
>your way... (which was what i had thought about)
>
>the issue of doing the string/list compare/search is that i can get
>everything from the db with one call... i can then iterate through memory
>for each of my row information that i'm searching to see if it exists in the
>db...
>
>memory searches should be faster than the network overhead, and the
>associated multiple db calls...


should... are you sure?
How many rows on the database? how many rows to compare? network overhead?
Do some timing/performance tests to evaluate that. Things aren't
always as you expect.



Gabriel Genellina
Softlab SRL





__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
seaching a txt file hgbso C++ 0 09-05-2008 11:54 PM
Seaching the data KK Java 2 06-21-2008 08:07 PM
seaching a file ruds Java 6 07-14-2007 04:11 AM
Seaching Active Directory via ADO LittlePython Python 10 02-19-2006 05:29 AM
Seaching word in text file using JAVA. imran Java 2 03-03-2005 02:23 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57