+ 11
Can I condense this?
Given two sentences, you need to find and output the number of the common words (words that are present in both sentences). Sample Input: this is some text I would like this tea and some cookies Sample Output: 2 The words 'some' and 'this' appear in both sentences. You can use the split() function to get a list of words in a string and then use the set() function to convert it into a set. For example, to convert the list x to a set you can use: set(x) ##MY CODE s1 = input() s2 = input() split1 = s1.split(" ") split2 = s2.split(" ") set1 = set(split1) set2 = set(split2) print (len(set1&set2)) ## 0ut of curiosity, are there other ways to do this?
17 Respuestas
0
setW = set(s1.split() + s2.split())
print (len(setW))
:)
+ 5
Candace Taylor
I enjoyed the answer from SoloProg so much that I had to offer another version:
print(len(set(input().split(" ")) & set(input().split(" "))))
+ 5
SoloProg Candace Taylor Rik Wittkopp Since a space is the default separator for the split method, you never need to write split(" "). It's simpler just to write split() which gives the same result. 🙂
+ 5
s1 = input()
s2 = input()
print(len(set(s1.split()) & set(s2.split())))
+ 4
David Ashton Thanks!
I always thought split() would create a list containing each letter.
I will play & learn
EDIT -> As I was playing, I realised that I knew this but had forgotten. 🤣😂
Which highlights the importance of continuous practice
+ 2
We could write everything in a single (if that is what you want)
+ 2
Candace Taylor ,
here are 2 samples doing it with other approaches:
# the basic way:
s1 = "lena tim lisa sandra tom john amy"
s2 = "bob mel tim barb lisa simon lena"
total = 0
for name in s1.split():
if name in s2:
total +=1
print(total)
# or doing it with a list comprehension:
print(sum([1 for name in s1.split() if name in s2]))
+ 1
Candace Taylor, SoloProg , I agree with David Ashton :
https://code.sololearn.com/c4jj180j31kX/?ref=app
0
Just wanted to see some other approaches/code that would do the same thing
0
Candace Taylor
Thanks for best answer Candace, but please return it to Soloprog as I just piggybacked off his concept.
😁👍
0
Didnt realize I did that but thanks to you both! I love seeing different ways to write the same code, it really helps me understand whats going on!
0
s1 = "lena tim lisa sandra tom john amy"
s2 = "bob mel tim barb lisa simon lena"
setW = set(s1.split() + s2.split())
print(len(set(input().split(" ")) & set(input().split(" "))))
0
print(len(set(input())&set(input())))
0
s1 = input()
s2 = input()
x1 = s1.split()
x2 = s2.split()
d = set(x1) & set(x2)
print(len(d))
0
s1 = input()
s2 = input()
splits1 = s1.split()
splist2 = s2.split()
sets1 = set(splits1)
sets2 = set(splist2)
commonset = (sets1 & sets2)
print(len(commonset))
0
s1 = input()
s2 = input()
s1.split()
s2.split()
i=0
L1= set(s1.split() )
L2 = set(s2.split() )
for w in L1:
for w1 in L2:
if w==w1:
i+=1
print(i)
- 1
s1 = input()
s2 = input()
split1 = s1.split(" ")
split2 = s2.split(" ")
set1 = set(split1)
set2 = set(split2)
print (len(set1&set2))