Solution Exercice 80: recherche de la liste des mots communs à deux texte en python

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:

(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

4 thoughts on “Solution Exercice 80: recherche de la liste des mots communs à deux texte en python

  1. 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.

Leave a Reply

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock