-1

I'm trying to find some function in Python which can help me with finding some word-matches of two different strings.

For example we have 2 strings:

  1. "I am playing basketball everyday"
  2. "basketball is the worst game ever"

And I want this function to return true if "basketball" was found in both strings.

shim
  • 9,289
  • 12
  • 69
  • 108
Pablo
  • 1

3 Answers3

2

You can find which are the common words in two phrases:

common_words = set(phrase1.split()).intersection(phrase2.split())

You can check if a word is in both phrases by simply checking if it is in the common_words set (example: if word in common_words: ...).

You can also check how many elements this set has. If len(common_words) == 0 then phrase1 and phrase2 contain no common words.

Riccardo Bucco
  • 13,980
  • 4
  • 22
  • 50
0
l = ["I am playing basketball everyday", "basketball is the worst game ever"]

for x in l:
  print (x)
  if "basketball" in x.lower():
    print (True)
Yatish Kadam
  • 454
  • 2
  • 11
0
str1 = "I am playing basketball everyday"
str2 = "basketball is the worst game ever"

if "basketball" in str1 and "basketball" in str2:
    print "basketball is in both strings!"

See: Python - Check If Word Is In A String

NahuelBrandan
  • 633
  • 6
  • 16