Find the problem of my list of objects in Python

1

Well, I'm asking you this question, since I've tried several ways, but I can not find the error in logic or the wrong use of some syntax in python.

It would be as follows, I have this class:

class PalavraIndices(object):

 def __init__(self,palavra):
    self.palavra = palavra
    self.items_in_Indices = {}

def __repr__(self):
    return str(self.palavra)

def add_item(self, arquivo_indice, qtd_repeticao):
    if bool(self.items_in_Indices) == False: # dicionario estiver vazio
        self.items_in_Indices[arquivo_indice] = qtd_repeticao
    else:            
        if not arquivo_indice in self.items_in_Indices: #nome do arquivo base não existir
            self.items_in_Indices[arquivo_indice] = qtd_repeticao
        else:
            self.items_in_Indices[arquivo_indice] += qtd_repeticao

def imprime_indices(self):
    conteudo = self.palavra + ":"
    arquivos = list( self.items_in_Indices.keys() )
    arquivos.sort()
    for arq in arquivos:

        conteudo = conteudo + " %s,%s" %(arq, self.items_in_Indices[arq])

    print(conteudo)

I'm adding words as objects to a list of objects to mark the quantity and in which file the word appears, having something like this at the end:

Car: (arq1,2), (arq2,1), (arq3,4)

I keep this format in the class, when I instantiate an object (word), within this object I use a dictionary.

I own three lists:

ListaA = ['modo', 'consulta', 'diversos', 'militantes', 'acarreta', 
'processo', 'reformulacao', 'modernizacao', 'fluxo', 'informacoes', 
'diversos', 'poderes', 'mundo']

ListaB = ['evidentemente', 'determinacao', 'clara', 'objetivos', 'promove', 
'alavancagem', 'niveis', 'motivacao', 'departamental']

ListaC = ['gostaria', 'enfatizar', 'percepcao', 'dificuldades', 'cumpre', 
'papel', 'essencial', 'formulacao', 'diretrizes', 'desenvolvimento', 
'futuro', 'papel', 'arvore']

This function checks whether the object already exists in the object list

   def busca_index(lista_objetos,palavra):
     contador = 0
     aux = 0
     for objeto in lista_objetos:
      if(objeto.palavra == palavra):
          return contador
        contador += 1
     return -1

Function in which I create the object list:

def guarda_word(lista_palavras,lista_objeto,indice):
  existe = -1
  for palavra in lista_palavras:
     objetoPalavra = PalavraIndices(palavra)
     if bool(lista_objeto) == False: # a list de objeto esta vazia
         objetoPalavra.add_item(indice,1)
         lista_objeto.append(objetoPalavra)
     else:
         existe = busca_index(lista_objeto,palavra)
         if(existe != -1):
             lista_objeto[existe].add_item(indice,1)
             existe = -1
         else:
            objetoPalavra.add_item(indice,1)
            lista_objeto.append(objetoPalavra)

and finally calls:

lista_objeto = []

guarda_word(ListaA,lista_objeto,'arquivoA')
guarda_word(ListaB,lista_objeto,'arquivoB')
guarda_word(ListaC,lista_objeto,'arquivoC')

print("lista de objeto")
print(lista_objeto)

for objeto in lista_objeto:
   objeto.imprime_indices()

Output generated is:

consulta
{'arquivoA': 13, 'arquivoB': 9, 'arquivoC': 13} 

But it is not counting correctly ... Anyway ... I do not know if you could understand, and I'm sorry if this question got big ... But if someone is interested in trying to find out why each object word is not computing correctly the quantities of times it repeats itself.

    
asked by anonymous 08.10.2017 / 04:27

2 answers

2

This whole complexity is not necessary when you are programming in Python !

This simple program is able to return what you want:

from collections import Counter

arquivos = [ 'texto1.txt', 'texto2.txt', 'texto3.txt' ]

def obter_palavras( arqs ):
    ap = []

    for a in arqs:
        with open( a,'r' ) as f:
            ap += [ (a,p) for p in f.read().split() ]

    cnt = Counter(ap).items()
    dic = { p:[] for a, p in ap }

    for p in dic.keys():
        for a in arqs:
            for k, n in cnt:
                if k == (a,p):
                    dic[p].append((a,n))

    return dic


print obter_palavras( arquivos )

texto1.txt

alpha beta gamma delta
zero um dois tres
kilo mega giga tera
terra agua ar fogo

texto2.txt

zero um dois tres
zero um dois tres
kilo mega giga tera

texto3.txt

alpha beta gamma delta
alpha beta gamma delta
kilo mega giga tera

Output:

{
    'mega': [('texto1.txt', 1), ('texto2.txt', 1), ('texto3.txt', 1)],
    'kilo': [('texto1.txt', 1), ('texto2.txt', 1), ('texto3.txt', 1)],
    'dois': [('texto1.txt', 1), ('texto2.txt', 2)],
    'zero': [('texto1.txt', 1), ('texto2.txt', 2)],
    'tera': [('texto1.txt', 1), ('texto2.txt', 1), ('texto3.txt', 1)],
    'terra': [('texto1.txt', 1)],
    'um': [('texto1.txt', 1), ('texto2.txt', 2)],
    'beta': [('texto1.txt', 1), ('texto3.txt', 2)],
    'ar': [('texto1.txt', 1)],
    'agua': [('texto1.txt', 1)],
    'delta': [('texto1.txt', 1), ('texto3.txt', 2)],
    'alpha': [('texto1.txt', 1), ('texto3.txt', 2)],
    'tres': [('texto1.txt', 1), ('texto2.txt', 2)],
    'fogo': [('texto1.txt', 1)],
    'giga': [('texto1.txt', 1), ('texto2.txt', 1), ('texto3.txt', 1)],
    'gamma': [('texto1.txt', 1), ('texto3.txt', 2)]
}
    
08.10.2017 / 20:06
1

I also think it's complicating the problem. I think using a dict would be a lot simpler. Search Google for the Python Counter class.

On your code, you are counting on the aux variable, but returning counter. The counter variable counts the words in the list and assumes the occurrences of the word in that list. So to get the output you want the right would be to return aux, right?

    
08.10.2017 / 07:22