-2
a = "This is some text"
b = "This text"

I want to compare the variable b in a and should return true even if the text contains more than two words like

a = "This is another text here"
b = "This another here"

even if I compare b in a should return true in python

Condition: True == All words in b found, the same order, in a.
In the above examples, the words have to be in the same order and upper/lowercase.

stovfl
  • 14,998
  • 7
  • 24
  • 51

2 Answers2

1
a = "This is some text"
b = "This text"

c = 0 
for i in b.split(): # check for each word in a
    if i in a: c = c + 1 # if found increases the count
c>=2 # true

or

len([value for value in a.split() if value in a.split()]) >= 2
Chandu
  • 2,053
  • 3
  • 25
  • 39
0

You can mimic the behavior to a certain extent using regex.

import re

def is_like(text,pattern):
    if '%' in pattern:
        pattern = pattern.replace('%','.*?')
    if re.match(pattern,text):
        return True
    return False


a = "This is another text here"
b = "This%another%here"
print(is_like(a,b))
>>>>True
a = "Thisis sometext"
b = "This % text"
print(is_like(a,b))
>>>>False
a = "Thisis sometext"
b = "This%text"
print(is_like(a,b))
>>>>True
a = "This is some text"
b = "This text"
print(is_like(a,b))
>>>>False

Note that I have not implemented any escaping for % character, so searching for % won't work.

Xnkr
  • 564
  • 5
  • 16