Can I condense this? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 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?

20th Aug 2021, 7:55 PM
Candace Taylor
Candace Taylor - avatar
17 Réponses
0
setW = set(s1.split() + s2.split()) print (len(setW)) :)
20th Aug 2021, 10:27 PM
SoloProg
SoloProg - avatar
+ 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(" "))))
20th Aug 2021, 11:35 PM
Rik Wittkopp
Rik Wittkopp - avatar
+ 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. 🙂
21st Aug 2021, 8:39 AM
David Ashton
David Ashton - avatar
+ 5
s1 = input() s2 = input() print(len(set(s1.split()) & set(s2.split())))
20th Feb 2022, 5:53 PM
Erika
+ 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
21st Aug 2021, 8:42 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
We could write everything in a single (if that is what you want)
20th Aug 2021, 8:03 PM
Lisa
Lisa - avatar
+ 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]))
21st Aug 2021, 6:28 AM
Lothar
Lothar - avatar
22nd Aug 2021, 10:40 AM
Raffaele Bisogno
Raffaele Bisogno - avatar
0
Just wanted to see some other approaches/code that would do the same thing
20th Aug 2021, 8:16 PM
Candace Taylor
Candace Taylor - avatar
0
Candace Taylor Thanks for best answer Candace, but please return it to Soloprog as I just piggybacked off his concept. 😁👍
20th Aug 2021, 11:39 PM
Rik Wittkopp
Rik Wittkopp - avatar
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!
20th Aug 2021, 11:41 PM
Candace Taylor
Candace Taylor - avatar
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(" "))))
10th Nov 2021, 4:34 PM
ANIKET SINGH
ANIKET SINGH - avatar
0
print(len(set(input())&set(input())))
14th Nov 2022, 12:45 PM
Raffaele Bisogno
Raffaele Bisogno - avatar
0
s1 = input() s2 = input() x1 = s1.split() x2 = s2.split() d = set(x1) & set(x2) print(len(d))
18th Nov 2022, 11:16 PM
Mohamed Gomaa Soliman
0
s1 = input() s2 = input() splits1 = s1.split() splist2 = s2.split() sets1 = set(splits1) sets2 = set(splist2) commonset = (sets1 & sets2) print(len(commonset))
8th Dec 2022, 11:27 AM
Pareshkumar Chaudhari
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)
13th Feb 2023, 1:21 PM
Castelo
- 1
s1 = input() s2 = input() split1 = s1.split(" ") split2 = s2.split(" ") set1 = set(split1) set2 = set(split2) print (len(set1&set2))
14th Nov 2022, 8:06 AM
Pooja Patel
Pooja Patel - avatar