searching 2nd item in each sublist - so close!

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: May 2004
Posts: 217
Reputation: marceta is an unknown quantity at this point 
Solved Threads: 0
marceta marceta is offline Offline
Posting Whiz in Training

searching 2nd item in each sublist - so close!

 
0
  #1
Apr 17th, 2008
Hey guys,

I'm working on a basic search engine and am really close to completion.

I currently have a function that takes a string and compares each word and its synonyms to a webpage.

My output at the moment is [("closeness" percentage of terms to webpage, webpage contents,(x,y),(x,y)...(x,y)]

I am almost there, but I now need to remove the items that have no match to a site (ie, where x = 0.

I have found out that the itemgetter() function isolated just the first variables, then I filtered out the zeros from there with this code

  1. def Google_search(string):
  2. internet_length = len(Internet)
  3. percentage_list = []
  4.  
  5. for x in range(0,internet_length):
  6. position = x
  7. closeness_percentage = closeness(string, Internet[x])
  8. percentage_list.append([closeness_percentage, Internet[position]])
  9.  
  10. sorted_list = sorted(percentage_list, key=operator.itemgetter(1), reverse = True)
  11. ## print sorted_list
  12.  
  13. ## now to delete the ones with zero percentage
  14.  
  15.  
  16. get_percentages = operator.itemgetter(0)
  17. percentages = map(get_percentages, sorted_list)
  18. print percentages
  19. no_zeros = [x for x in percentages if x is not 0]
  20. print no_zeros
  21. print sorted_list

So any example of the output would be
[13, 0, 3, 2, 0, 0, 4, 0, 0, 6, 2, 3, 0, 0]
[13, 3, 2, 4, 6, 2, 3]

This is good, however, deleting the zeros from percentage only list does not correlate to them being deleted from the list with the webpages - obviously as its a new list!

I have been straining my brain for hours about how to get around this! I think I need to make a loop that compares the 2nd value in each SUBLIST to the values of the original list, then if its a match return true, then filter the results! But i dont know how to do something like

  1. for x in range(0, length):
  2. for y in range(0, no_zeros_length):
  3. if sorted_list[x].itemgetter(1) == no_zeros:
  4. return true

Do you guys get what I mean? Or is there a much easier way to omit the zeros from the original list?

Thanks heaps in advance!

ps. Ive attached the file (rename to .py if you want to use it)..so its easier to understand whats going on as this is part 4 and each part is dependant on the others before it (thought it would be too much code for a post)!

or get them here

Python File

As .txt
Last edited by marceta; Apr 17th, 2008 at 8:53 pm.
Attached Files
File Type: txt ASSIGNMENT_almost_done.txt (25.0 KB, 8 views)
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: searching 2nd item in each sublist - so close!

 
0
  #2
Apr 18th, 2008
I think you want a dictionary, if I understand correctly. The dictionary is the standard way of mapping one set of items to another.

So you have

mydict = {URL1: 13, URL2: 0, URL3: 3, URL4: 2, ...}

And then you run this bit of code:

  1.  
  2. for URL in mydict.copy():
  3.  
  4. if mydict[URL] == 0:
  5. mydict.pop(URL)

and then your list of hot URLs is simply mydict.keys().

Jeff
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC