![]() |
| ||
| Need Help With a Function.... def checkIt(arr,inp): 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! |
| ||
| 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 :) |
| ||
| Re: Need Help With a Function.... Thanks for your response--- I'll try it later. What does list() do? |
| ||
| Re: Need Help With a Function.... sorry, I meant inp.split(). The reason is that "hi" in ["What", "a", "high", "mountain"]is False ! |
| ||
| Re: Need Help With a Function.... That works, but what if a value in the array is more than one word? ['hey there'] |
| ||
| Re: Need Help With a Function.... There is a solution with the remodule. You can build a regular expression by joining all the elements of array array = ["hi", "hey there", "Hello world"]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: 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): |
| ||
| Re: Need Help With a Function.... If you don't fully understand the remodule (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. |
| ||
| Re: Need Help With a Function.... If I merge array values into one array.. value.. what is the point of my function? |
| ||
| 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