DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Python (http://www.daniweb.com/forums/forum114.html)
-   -   Need Help With a Function.... (http://www.daniweb.com/forums/thread140726.html)

Dekudude Aug 17th, 2008 12:48 pm
Need Help With a Function....
 
def checkIt(arr,inp):
        bool = 0
        for i in arr:
                if i in inp:
                        bool = 1
                        break
        return bool

array = ['hi','hey','hello']
string = "Hi there!"

if checkIt(array,string)
        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!

Gribouillis Aug 17th, 2008 1:00 pm
Re: Need Help With a Function....
 
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 :)

Dekudude Aug 17th, 2008 1:05 pm
Re: Need Help With a Function....
 
Thanks for your response--- I'll try it later.

What does list() do?

Gribouillis Aug 17th, 2008 1:09 pm
Re: Need Help With a Function....
 
sorry, I meant
inp.split()
. The reason is that
"hi" in ["What", "a", "high", "mountain"]
is False !

Dekudude Aug 17th, 2008 10:13 pm
Re: Need Help With a Function....
 
That works, but what if a value in the array is more than one word?

['hey there']

Gribouillis Aug 18th, 2008 2:14 am
Re: Need Help With a Function....
 
There is a solution with the
re
module. You can build a regular expression by joining all the elements of array
array = ["hi", "hey there", "Hello world"]
myPattern = re.compile("|".join([re.escape(w) for w in array]))
checkIt = myPattern.search

if checkIt("foo bar buzz hey there, what are you doing?"):
  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...

Dekudude Aug 18th, 2008 8:16 pm
Re: Need Help With a Function....
 
What changes should I make to my function? You aren't very clear as to where..

def checkIt(arr,inp):
        inp = input.split(' ')
        bool = 0
        for i in arr:
                if i in inp:
                        bool = 1
                        break
        return bool

shadwickman Aug 18th, 2008 10:58 pm
Re: Need Help With a Function....
 
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.

Dekudude Aug 18th, 2008 11:20 pm
Re: Need Help With a Function....
 
If I merge array values into one array.. value.. what is the point of my function?

shadwickman Aug 18th, 2008 11:21 pm
Re: Need Help With a Function....
 
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...


All times are GMT -4. The time now is 1:53 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC