Need Help With a Function....

Reply

Join Date: Jul 2008
Posts: 33
Reputation: Dekudude is an unknown quantity at this point 
Solved Threads: 1
Dekudude Dekudude is offline Offline
Light Poster

Need Help With a Function....

 
0
  #1
Aug 17th, 2008
  1. def checkIt(arr,inp):
  2. bool = 0
  3. for i in arr:
  4. if i in inp:
  5. bool = 1
  6. break
  7. return bool
  8.  
  9. array = ['hi','hey','hello']
  10. string = "Hi there!"
  11.  
  12. if checkIt(array,string)
  13. print "Yay!";

That's my code. It returns, "Yay!". Now... I like what it does-- it searches the string for a value in an array, and if it finds it, returns true. However, if the string were "What a high mountain!", it would still return true. As you can see, in "high", the characters 'hi' can be found. I'd like it to search for a whole word, rather than a couple characters. How could I do this? Thanks!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 893
Reputation: Gribouillis has a spectacular aura about Gribouillis has a spectacular aura about Gribouillis has a spectacular aura about 
Solved Threads: 209
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Practically a Posting Shark

Re: Need Help With a Function....

 
0
  #2
Aug 17th, 2008
You could add inp = list(inp) as the first line in your function.
Also you should avoid using names like 'bool', 'array' or 'string' (type or module names) as variable names
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 33
Reputation: Dekudude is an unknown quantity at this point 
Solved Threads: 1
Dekudude Dekudude is offline Offline
Light Poster

Re: Need Help With a Function....

 
0
  #3
Aug 17th, 2008
Thanks for your response--- I'll try it later.

What does list() do?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 893
Reputation: Gribouillis has a spectacular aura about Gribouillis has a spectacular aura about Gribouillis has a spectacular aura about 
Solved Threads: 209
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Practically a Posting Shark

Re: Need Help With a Function....

 
0
  #4
Aug 17th, 2008
sorry, I meant inp.split() . The reason is that
  1. "hi" in ["What", "a", "high", "mountain"]
is False !
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 33
Reputation: Dekudude is an unknown quantity at this point 
Solved Threads: 1
Dekudude Dekudude is offline Offline
Light Poster

Re: Need Help With a Function....

 
0
  #5
Aug 17th, 2008
That works, but what if a value in the array is more than one word?

['hey there']
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 893
Reputation: Gribouillis has a spectacular aura about Gribouillis has a spectacular aura about Gribouillis has a spectacular aura about 
Solved Threads: 209
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Practically a Posting Shark

Re: Need Help With a Function....

 
0
  #6
Aug 18th, 2008
There is a solution with the re module. You can build a regular expression by joining all the elements of array
  1. array = ["hi", "hey there", "Hello world"]
  2. myPattern = re.compile("|".join([re.escape(w) for w in array]))
  3. checkIt = myPattern.search
  4.  
  5. if checkIt("foo bar buzz hey there, what are you doing?"):
  6. print "Yay!"
However, I'm not sure its so efficient if the array is large. For example, if the elements of the array are sequences of words separated by white space, it could be better to look for all words and blocks of white space in the string...
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 33
Reputation: Dekudude is an unknown quantity at this point 
Solved Threads: 1
Dekudude Dekudude is offline Offline
Light Poster

Re: Need Help With a Function....

 
0
  #7
Aug 18th, 2008
What changes should I make to my function? You aren't very clear as to where..

  1. def checkIt(arr,inp):
  2. inp = input.split(' ')
  3. bool = 0
  4. for i in arr:
  5. if i in inp:
  6. bool = 1
  7. break
  8. return bool
Last edited by Dekudude; Aug 18th, 2008 at 8:19 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: Need Help With a Function....

 
0
  #8
Aug 18th, 2008
If you don't fully understand the re module (I don't), you could always have it look for the first word in that two-word value (in the array), and then if it found the first one, see if the word following it in the input is the same as the second word in the two-word value. Sorry if I didn't make that very clear. But anyways, I think that Gribouillis' idea would be the best, as it's straight-forward and clean looking.
Last edited by shadwickman; Aug 18th, 2008 at 11:02 pm.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson

my photography
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 33
Reputation: Dekudude is an unknown quantity at this point 
Solved Threads: 1
Dekudude Dekudude is offline Offline
Light Poster

Re: Need Help With a Function....

 
0
  #9
Aug 18th, 2008
If I merge array values into one array.. value.. what is the point of my function?
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: Need Help With a Function....

 
0
  #10
Aug 18th, 2008
Let me get one thing straight: Are any of the array values ever going to be more than one word long? Because earlier in this thread you asked how to deal with it in that situation...
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson

my photography
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC