3
I would like to know a way to correlate equal words in different txt files. It will read the words of a txt file and look for those words in the other txt file.
3
I would like to know a way to correlate equal words in different txt files. It will read the words of a txt file and look for those words in the other txt file.
2
To see the elements in common between two files (word lists) makes the intersection between your sets:
with open('arquivo1.txt') as f1, open('arquivo2.txt') as f2:
content1 = f1.read().split() # dividir por palavras
content2 = f2.read().split() # dividir por palavras
comuns = set(content1) & set(content2) # {paravras, comuns, nos, dois, arquivos}
0
Well... I’d do it this way:
First it would save the contents of each file in a list, using
lista = file.read()
, then make a newlista = lista.split()
which would take each word and place it in a list position, which would result in something like this:
conjunto_A = ['oi','tchaum','ontem','amanha']
conjunto_B = ['oi','tchaum','ontem','amanha','tarde','noite']
palavras_C = [item for item in conjunto_A if item in conjunto_B]
print(palavras_C)
having word lists is just checking which words on list A also belong to the word set on list B
in this example I gave, the output is:
['oi', 'tchaum', 'ontem', 'amanha']
only the elements that the two lists have.
Browser other questions tagged python
You are not signed in. Login or sign up in order to post.
mano to creating a list of objects here... but when I print this list of objects is giving a strange exit... you know how to do this?
lista_Objetos = []


def cria_lista_objetos(word,indiceArq,qtd):
 lista_ObjetosFun = []
 nomeWord = PalavraIndices(word)
 nomeWord.add_item(indiceArq,qtd)
 lista_ObjetosFun.append(nomeWord)
 return lista_ObjetosFun

for i in palavras_A:
 lista_Objetos = cria_lista_objetos(i,'arquivo1',1)
and this is the way out:[<__main__.PalavraIndices object at 0x02B39D50>]
– William Henrique
I’ll put as a question then... here in the comment it is difficult to even understand..
– William Henrique
I thought when printase the Objects list would show the name of each instantiated object inside the list... and then I would write to a file.. each object of that, going through the list
– William Henrique
@Williamhenrique but you have only one object in the list. When
lista_Objetos = cria_lista_objetos(i,'arquivo1',1)
, you are simply replacing the value. you must want tolista_Objetos.append(...)
– Miguel
I went up a question there.. because I tried to understand here and I could not... from a look there.. name of the question is: List of objects in Python
– William Henrique