Python-courses-python-exercises-python-dictionary

Exercice 80

Write a python algorithm which determines the list of all common words to two text T1 and T2 without repetition. Example if:
T1 = "Python is open source programming language. Python was created on 1991"
and
T2 = "Python is the most popular programming language "
, the algorithm returns the liste

['Python', 'is', 'programming', 'language.']

( the word 'Python' must be added only once even if it shared twice)

Solution

def commonWords(T1 , T2):
# converting the texts into lists
listWords_in_T1 = T1.split()
listWords_in_T2 = T2.split()
print(listWords_in_T2)

# initializing the common words
listCommon_words = []
for word in listWords_in_T1 :
if word in listWords_in_T2 and word not in listCommon_words:
listCommon_words.append(word)
return listCommon_words

# Example
T1 = "Python is open source programming language. Python was created on 1991"
T2 = "Python is the most popular programming language. "
print("The list of common words is : " , commonWords(T1 , T2))
# The output is :
# The list of common words is : ['Python', 'is', 'programming', 'language.']

Younes Derfoufi
my-courses.net

Leave a Reply