I am building a simple function to search through some data from the list.
I am trying to search string from list matches some characters from start of string
.
Like :-
JavaScript
x
10
10
1
my_list = ["com truise", "bill james", "bill bates", "bustin jeiber"]
2
3
def search_from_list(searched_word):
4
for word in my_list:
5
if searched_word == word:
6
print("Matched")
7
print("All words" + __full_word_that_matched__)
8
9
search_from_list("bi")
10
and if user calls the function with argument "bi"
then I am trying to get strings which matched that string "bi"
like :-
JavaScript
1
2
1
["bill james", "bill bates"]
2
But it is not working, I have tried many methods like :-
JavaScript
1
7
1
def search_from_list(searched_word):
2
my_list = ["com truise", "bill james", "bill bates", "bustin jeiber"]
3
4
print re.findall(r"(?=("+'|'.join(my_list)+r"))", searched_word)
5
6
searched_word("bi")
7
But it returned empty
then I tried
JavaScript
1
6
1
def search_from_list(searched_word):
2
words = re.findall(r'w+', searched_word)
3
return [word for word in words if word in my_list]
4
5
n = search_from_list("bi")
6
It also showed me empty list
Any help would be much Appreciated. Thank You in Advance
Advertisement
Answer
JavaScript
1
5
1
my_list =
2
3
def search_from_list(substring):
4
return [string for string in my_list if substring in string]
5