1
Hello!
In Wendel Melo’s book, Introduction to the Python Programming Universe, I tried to do the last exercise, but I’m not getting it.
Enunciation:
Make a program that reads three user strings and list each word that appears at least once in one of the strings. Each word only should be listed only once, and next to it the texts in which the same appears. Your program must include a function that receives strings read and return a dictionary wave the keys are composed by the words and items are sets indicating the texts in which each key (word) appears.
Example:
I made this code, it presents the respective items of text1, text2 and text3, but I do not know how to follow, because the words repeat during the code. I believe that a good solution would be to use the intersection, set method, somewhere in the code, but I don’t know how to turn that intersection into words text 1, text2 and text 3, because the method will only return the element of the intersection and not the set where it is. From now on, thank those who help me.
def tDicionario(texto1, texto2, texto3):
palavras = {}
for palavra in texto1:
print('{}: '.format(palavra))
for palavra in texto2:
print('{}: '.format(palavra))
for palavra in texto3:
print('{}: '.format(palavra))
if __name__ == '__main__':
texto1 = input('Entre com o texto 1: ').split()
texto2 = input('Entre com o texto 2: ').split()
texto3 = input('Entre com o texto 3: ').split()
print('Listagem de palavras: ')
print(tDicionario(texto1, texto2, texto3))