Return one or more values from a list if they appear in a substring
I have a list and a string:
lst = ['rock','paper','scissor','green apple', 'red apple']
I want to create a function that returns a string of items from the list if they appear as substrings in the text
So for example:
lst = ['rock','paper','scissor','green apple', 'red apple']
def my_func(string):
.
.
.
return print(new_string)
#------------
string1 = 'we like rock and green apples'
string2 = 'i wrote on paper'
string3 = 'we played rock, paper, scissors, and had apples'
my_func(string1)
my_func(string2)
my_func(string3)
rock, green apples
paper
rock, paper, scissor
Here you go!
lst = ['rock','paper','scissor','green apple', 'red apple']
def my_func(string):
new_string = ''
for i in lst:
if i in string:
new_string += i + ', '
return print(new_string)
string1 = 'we like rock and green apples'
string2 = 'i wrote on paper'
string3 = 'we played rock, paper, scissors, and had apples'
my_func(string1)
my_func(string2)
my_func(string3)