문제

I have a message of text and a list of terms. I'd like to create an array that shows which terms are found in the message. For instance:

message = "the quick brown fox jumps over the lazy dog"
terms = ["quick", "fox", "horse", "Lorem", "Ipsum", "the"]

result = idealMethod(message, terms)
=>   [1,1,0,0,0,1] 

Because "quick" was the first item in the list of terms and was also in the message a 1 is placed in the first position in the result. Here's another example:

message2 = "Every Lorem has a fox"
result2 = idealMethod(message2, terms)
=> [0,1,0,1,0,0]

Update: The terms need to be exact matches. For instance if my search term include sam I don't want a match for same

도움이 되었습니까?

해결책

I think you want:

words = set(message.split(" "))
result = [int(word in words) for word in terms]

Note that split() splits on whitespace by default, so you could leave out the " ".

다른 팁

You can use list comprehension and leverage fact, that True is evaluated as 1 in numeric context:

words = set(message.split())
result = [int(term in words) for term in terms]
Out[24]: [1, 1, 0, 0, 0]

EDIT changed to look only for whole word matches, after clarification.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top