Exercice 80
Ecrire un algorithme python qui détermine la liste de tous les mots communs à deux textes T1 et T2
sans répétition.
Exemple si:
T1 = "Python is open source programming language. Python was created on 1991" et T2 = "Python is the most popular programming language ", l'algorithme renvoie la liste:
|
['Python', 'is', 'programming', 'language.'] |
(le mot 'Python' ne doit être ajouté qu'une seule fois même s'il est partagé plusieurs fois)
Solution
def commonWords(T1 , T2):
# convertir les textes en des listes
listWords_in_T1 = T1.split()
listWords_in_T2 = T2.split()
# initialisation de la liste des mots communs
listCommon_words = []
# rechercher les mots communs à T1 et T2 et les ajouter à la liste 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
# Exemple
T1 = "Python is open source programming language. Python was created on 1991"
T2 = "Python is the most popular programming language. "
print("La liste des mots communs est : " , commonWords(T1 , T2))
# La sortie est :
# La liste des mots communs est : ['Python', 'is', 'programming', 'language.']
Younes Derfoufi
CRMEF OUJDA
Post Views: 4 513
Your method of telling all in this post is actually pleasant, all can effortlessly understand it, Thanks a lot.
Hey there! I just wanted to ask if you ever have any issues with hackers?
My last blog (wordpress) was hacked and I ended up losing a few months
of hard work due to no backup. Do you have any methods to prevent
hackers?
Wow, incredible blog structure! How lengthy have you ever
been running a blog for? you made blogging look easy. The overall look
of your website is magnificent, let alone the content!
I do accept as true with all of the ideas you’ve introduced
on your post. They are very convincing and can definitely work.
Still, the posts are very quick for starters. Could you please extend them a
little from next time? Thanks for the post.