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

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

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

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

Your email address will not be published. Required fields are marked *