Find the problem in my Python object list

Asked

Viewed 195 times

1

Well I’m asking this question, because I’ve tried several ways, but I can’t find the error in logic or the wrong use of some syntax in python.

It would be as follows, I own 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 like objects to a list of objects to mark the amount and in which file the word appears, having something like this at the end:

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

I store in this format in the class, when I instate an object(word), inside this object I use a dictionary.

I have 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 if 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 where I create the list of objects:

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 the 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()

The generated output is:

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

but it is not counting correctly... Anyway.. I do not know if I could understand, and I’m sorry if it became giant this question... But if someone is interested in trying to find out why each object word is not correctly computing the amounts of times it repeats.

  • 1

    I think you’re making a simple problem too difficult, you necessarily want to do it by POO or it can be otherwise?

  • then @Ruyneto is because using this object, the ways I thought, would be the easiest way to record the information of each word in that output format. and in this case I do not see the object as a complicator but a facilitator.. the problem is in this function that I list the objects, because by my tests the problem is not in the class, but in this function guarda_word that I think there must be some wrong reference. for example... instance this class and add items to it and then print objeto.imprime_indices() you will notice that this accounting correctly

  • @Ruyneto discovered the problem! the error is in def __init__(self,palavra): I’ll update td for you to see

  • Good morning. The output of the program here https://repl.it/MQXR/2 is nothing like the one you put in the question.

  • 1

    @Miguel is because as I mentioned in the comments above.. I managed to solve the problem and updated the code in the question to what is working

  • @Williamhenrique ok, I get it

Show 1 more comment

2 answers

3


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

Exit:

{
    '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)]
}

1

I also think you’re complicating the problem. I think using a Dict would be much simpler. Search Google for the Python Counter class.

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

  • discovered the problem! the error is in def init(self, word):

  • now the code is correct.. I fixed it.. it’s... it’s working properly

Browser other questions tagged

You are not signed in. Login or sign up in order to post.