-1

You will be given two sentences S1, S2 your task is to find

  • a. Number of common words between S1, S2
  • b. Words in S1 but not in S2
  • c. Words in S2 but not in S1

I cant seem to do this problem for words , but I can do it for letters. Please help me out

example

S1= "the first column F will contain only 5 unique values"
S2= "the second column S will contain only 3 unique values"

Output:

a. 7
b. ['first','F','5']
c. ['second','S','3']
MisterMiyagi
  • 44,374
  • 10
  • 104
  • 119
  • ([python - How to split a string into a list? - Stack Overflow](https://stackoverflow.com/questions/743806/how-to-split-a-string-into-a-list)) – user202729 Feb 22 '21 at 10:12

1 Answers1

0

Use set.

a_words = set(S1.split(" "))
b_words = set(S2.split(" "))

A_ANS = len(a_words.intersection(b_words))
B_ANS = list(a_words - b_words)
C_ANS = list(b_words - a_words)
Mont
  • 164
  • 1
  • 4
  • I tried to run the code and this happened "TypeError: unhashable type: 'list' – Tanmay Sharma Feb 22 '21 at 15:04
  • def string_features(S1, S2): a_words = {S1.split(" ")} b_words = {S2.split(" ")} a = len(a_words.intersections(b_words)) b = list(a_words - b_words) c = list(b_words - a_words) return a,b,c S1= "the first column F will contain only 5 uniques values" S2= "the second column S will contain only 3 uniques values" a,b,c = string_features(S1, S2) print(a) print(b) print(c) – Tanmay Sharma Feb 22 '21 at 15:05
  • I have changed code a bit, please try again. – Mont Feb 22 '21 at 15:48