I have data which looks something like
data = [('h'), (('j','kjjy')), ((('khg','hello')))]
I would like to be able to recursively search for 'hello' and just return true or false but I am not sure how to do it.
I have data which looks something like
data = [('h'), (('j','kjjy')), ((('khg','hello')))]
I would like to be able to recursively search for 'hello' and just return true or false but I am not sure how to do it.
Here is an example:
import numpy as np
array = np.asarray(a)
def searchForWord(array, word):
#base case
if (array.size == 0) return false
#recursive case
if word in array[0] return true
array = np.delete(array, 0)
searchForWord(array, word)
You may have an issue because the numpy array would be an array of tuples. Just let me know if it works.
a=[('h'), (('j','kjjy')), ((('khg','hello')))]
for x in range(len(a)):
tup = a[x]
print(tup)
print('hello' in tup)
Output:
h
False
('j', 'kjjy')
False
('khg', 'hello')
True
You can do this recursively:
def find(text, data):
# loop over all items (tuples or strings)
for item in data:
# if it's a tuple, recurse
if isinstance(item, tuple):
if find(text, item):
return True
# if it's a string and a match return true
if item==text:
return True
return False