| | |
Need Help With a Function....
![]() |
•
•
Join Date: Jul 2008
Posts: 33
Reputation:
Solved Threads: 1
Python Syntax (Toggle Plain Text)
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!
sorry, I meant
is False !
inp.split() . The reason is that python Syntax (Toggle Plain Text)
"hi" in ["What", "a", "high", "mountain"]
There is a solution with the
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...
re module. You can build a regular expression by joining all the elements of array python Syntax (Toggle Plain Text)
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!"
•
•
Join Date: Jul 2008
Posts: 33
Reputation:
Solved Threads: 1
What changes should I make to my function? You aren't very clear as to where..
Python Syntax (Toggle Plain Text)
def checkIt(arr,inp): inp = input.split(' ') bool = 0 for i in arr: if i in inp: bool = 1 break return bool
Last edited by Dekudude; Aug 18th, 2008 at 8:19 pm.
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
- Hunter S. Thompson
my photography
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
- Hunter S. Thompson
my photography
![]() |
Similar Threads
- Need help on a program (function bug) (C)
- Assign content to a function pointer (C)
- PHP Feezing with a function (PHP)
- ASP Replace Function.... (ASP)
- missing function header (C++)
- Function that returns void (C++)
Other Threads in the Python Forum
- Previous Thread: New to Python, help printing output of system calls
- Next Thread: changing GUI screens?
| Thread Tools | Search this Thread |
abrupt ansi anti apache approximation array assignment avogadro backend beginner binary bluetooth book builtin calculator character code converter countpasswordentry curved customdialog dan08 dictionaries dictionary dynamic examples exe file float format function gnu graphics gui heads homework ideas import inches input java launcher library line lines linux list lists loop mouse mysqlquery number numbers numeric output parsing path phonebook plugin pointer port prime programming progressbar projects py2exe pygame python random recursion redirect scrolledtext software statictext statistics string strings sum table terminal text textarea thread threading time tlapse trick tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable wordgame write wxpython xlib





