I have a list of sentences which I need to check against another list containing some phrases. I have to check if each of the sentence in first list contains one of the phrases from the second list. If it has, then I need to assign a score of 1 to that sentence. If it doesn't then I need to assign a score of 0. For example: If the list of sentences are list_1=['a recent study found that pomegranate juice can be used to help alleviate prostate cancer','researchers have also noted that pomegranates help slow the spread of hormone-dependent cancers','this limits their ability to multiply']
And the list of phrases are list_2=['found that','noted that']
Then I need to give all the sentences in list number 1 a score based on the condition that the sentences contain a phrase from the second list. If it has a phrase, then it should give a score of 1 and if it doesn't, it should give a score of 0. For the above example, the first two sentences should get a score of 1 as they contain the phrases from the second list whereas the last sentence should get a score of 0 as it doesn't contain any phrase.
You can solve the above problem by following code:
list_1=['a recent study found that pomegranate juice can be used to help alleviate prostate cancer','researchers have also noted that pomegranates help slow the spread of hormone-dependent cancers','this limits their ability to multiply']
list_2=['found that','noted that']
count_dict={}
for l in list_1:
c=0
for l2 in list_2:
if l.find(l2)!=-1:#then it is a substring
c=1
break
if c:#
count_dict[l]=1
else:
count_dict[l]=0
In the above code l.find(l2)
checks if l2
is substring of l
. If it is, then it returns a value which is not -1
. You can also check string.count(substring)
which should be greater than 0
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.