-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
  • 8,356
  • 10
  • 70
  • 102
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
  • 11,231
  • 3
  • 17
  • 42
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
  • 434
  • 2
  • 10
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

Nahuel Brandán
  • 504
  • 5
  • 15