I have the following list
JavaScript
x
2
1
l=['web','python']
2
and the following string
JavaScript
1
2
1
st='python, C, something, find'
2
I want to check if any element in the list is in the string
I tried using
JavaScript
1
2
1
any(x in l for x in st)
2
however it gives false
seems python can’t recognize the element in the string, is there a way to check elements from list to a string if there is a match. i did it successfully using a for a loop and splitting the string to a list however that is not efficient with the real data as it contain a long lists and strings. need something efficient. thanks
Advertisement
Answer
You can use this method
JavaScript
1
6
1
word_list = ['web','python']
2
string = 'python, C, something, find'
3
4
exist = filter(lambda x : x in string , word_list)
5
print(list(exist))
6
output:
JavaScript
1
2
1
['python']
2