943,678 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 553
  • Python RSS
Oct 12th, 2008
0

Effiency in list searching

Expand Post »
A real simple one, just a question of understanding the python syntax I suppose. I won't waste your time by throwing the whole program at you, I think my question can be boiled down to this:

a = ["cat"]
b = ["bark"]
wordinsa = input("Word: ")
a.append(wordinsa)
wordinsb = input("Word: ")
b.append(wordinsb)
wordq = input("Try: ")
if wordq in a:
    c = a.index(wordq)
    print wordq
    print
    print b[c]
else:
    print "Nope"

Basically a dictionary built out of lists; the real thing jumps to back to main menu, works real nice actually.
But I was thinking just now, when the program checks for matches in list a, it compares the entire strings of wordq and the ones in the list. If one were to rewrite it a little, perhaps like this:
maybe add in another if-statement checking if the first letter of the word is present?
maybe add a loop checking letter for letter?

...would it be possible to gain a little extra effiency this way, though the program is longer?
After all, the lists in the real thing can be as long as any Britannica Encyclopedia you'd care to name.
I know about dictionaries and tuples and so on, this is more of an attempt to get a general grasp of the flow in a computer program!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Devlan is offline Offline
13 posts
since Oct 2008
Oct 12th, 2008
0

Re: Effiency in list searching

You are assuming the word at index in list 'a' makes sense with the word with the same index in list 'b'. Somewhat dangerous. It would be simpler to make a dictionary with a:b (key:value) pairs, this way things that belong together stay together. Also, dictionary searches are highly optimized and very fast.
Last edited by vegaseat; Oct 12th, 2008 at 12:44 pm.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 12th, 2008
0

Re: Effiency in list searching

Definitely use a dictionary or SQL database if there are a lot of entries. It is hundreds or thousands of times faster depending. The time consuming part of the code is looping through the list twice. Using a try/except to check the list once with no "in" statement is a workaround, but just a workaround and not better than using a dictionary. As for checking letter by letter, that's what the existing or any compare/find/index has to do. There is no way to find something comparing an entire word to an entire word. The only shortcut that I know of would be to group words according to length. Thus, a five letter word would only look at other 5 letter words, and not words of other lengths.
Python Syntax (Toggle Plain Text)
  1. ##if wordq in a: ## omit this line - duplicate list checking
  2. try:
  3. c = a.index(wordq)
  4. print wordq
  5. print
  6. print b[c]
  7. except:
  8. print wordq, "wasn't found in 'a'"
Edit: There is also sets if dictionaries are more than you want to do at this time. Both dictionaries and sets use a hash as an index, so the execution times should be similiar for both.
Python Syntax (Toggle Plain Text)
  1. list1 = ['a', 'b', 'c', 'd']
  2. list2 = ['b']
  3. set1=set(list1)
  4. set2=set(list2)
  5. set3= set1.intersection(set2)
  6. print "set3 =", set3
  7.  
  8. set5=set(["z"])
  9. set6=set1.intersection(set5)
  10. print "set6 =", set6
Last edited by woooee; Oct 12th, 2008 at 2:34 pm.
Reputation Points: 741
Solved Threads: 691
Nearly a Posting Maven
woooee is offline Offline
2,302 posts
since Dec 2006
Oct 12th, 2008
0

Re: Effiency in list searching

Yea, I realise using lists for this purpose isn't exactly the optimal way of going about it, but it was part of a task a while back and it just got me thinking in those tracks... how does this work, how could you make this particular method work more efficently.

Thanks for the input woooee, I wasn't aware of that "intersect"-command. So much to learn...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Devlan is offline Offline
13 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Problem with nested list
Next Thread in Python Forum Timeline: Installer Programs





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC