Basic programming help in python

Asked

Viewed 57 times

0

Good afternoon, I created a program that needs to sort the numbers and write them in string form, the problem occurs after I order, it seems to be returning an empty list, because when I try to run toString, when I debuggo I realize that it always takes an empty list...

My thought was, I’ll go through the existing numbers, find the smallest, play on a new list and delete the smallest number from the previous list and return to the new list.

I accept any tips because I’m a beginner.

class Ordenacao():

   def __init__(self, array_para_ordenar:[]):     
       self.__lista = array_para_ordenar      

   def ordena(self):
       array = self.__lista
       lista1 = []
       for j in range(len(array)):
           pos = 0
           for i in range(1, len(array)):
               if array[pos] > array[i]:
                   pos = i
           lista1.append(array[pos])
           array.remove(array[pos])
       return lista1
       
   def toString(self):
       array = self.__lista
       for i in range(len(array)):
           array[i] = str(array[i])
       str1 = ','.join(array)
       return (str1)
       
   
       
teste = [9, 0, 1]
orde = Ordenacao(teste)
orde.ordena()
orde.toString()
  • I think the problem is your job ordena it seems that it returns an ordered array, it removes the items from the array self.__lista in the process. I think in the end it should replace the self.__lista with the new ordered array.

  • @Victoraraújo Little locust, I don’t know Python, but you can see that 1) if the ordering and return of the ordered list is correct, then when calling orde.ordena() You’re not doing anything with what this call returns, you’re not keeping it on another list or anything. 2) You may not need to return anything, you could just point to __lista that keeps in the object to the one that you ordered, within the concept of updating fields of objects (that personally do not find good beginner mess at the beginning of the programming study, because POO is something kind of advanced, but anyway, is fashionable).

  • Where in the code array = self.__lista replace with array = self.__lista[:] and at the end of the code do where you are orde.ordena() do print(orde.ordena())

  • Although the titles of the questions suggested above are different, the basic problem is the same: you are modifying the original list, as already explained in the comments (which also gave the solution)

  • Solution no, only a repair the solution goes beyond the scope of the question.

  • Well, from what the AP said, the problem is that he’s trying to print out a list that he emptied himself, according to the logic he gave in the question he used to sort. All that was missing was to play the ordered list over the emptied list as well (in case you do not need to copy the items one by one, just point to the variable __lista for the list that was ordered), there the toString() you’ll have a better chance of working.

  • When you do array2 = array1, everything you do on array2 will happen in the array1 also. You have to clone if you want to create an independent copy: array2 = array1.copy()

  • 1

    Taking advantage, in Python there is the special method __str__ that you can use instead of toString. And I understand that you may want to sort manually (maybe to train), but in real systems, prefer to use what you already have ready: https://ideone.com/GsRZ9L

  • 1

    @Natanfernandes A lista1 where are going the items ordered by what I understand does not point to the original, is another list, so what I said should work, just do __lista = lista1 at the end of the ordination.

  • @hkotsubo, the one you presented is the solution.

  • 2

    @Augustovasques I reread the question several times and my understanding is that the main/specific problem is the fact that the list is empty at the end (and the reason is addressed in the dups indicated). Everything else (__str__, the sorting algorithm itself, etc.), I understand it is secondary, so I keep closing (by the way, if anyone has other dup suggestions, you can comment that I add). But of course, if someone disagrees with the closure, just use the usual means (vote to reopen, open discussion on [goal], etc)

  • Well, I believe I understood where I went wrong, it would be because I should not delete the elements of the array that is conditioned in my constructor method, right? What I did to solve was kind of a gambit in oderna() and put self. __list = list1 before returning. Thank you all for the tips

  • and @Augustovasques, could you explain to me, please explain why you need to use the [:] in the = self array. __list[:]

  • 1

    @hkotsubo, I do not disagree and do not want to reopen the question because the question is implicitly broad, because initially it presents the problem mentioned but brings with it several errors of concept that should be resolved and carefully in a solution. That’s why I don’t consider my code as a solution but as a repair to what the author is already doing while their code yes eliminates those problems that bother me.

  • @Victoraraújo this code makes a superficial clone of self.__lista.

  • 1

    @Augustovasques show!! did not know this, the part of Class already came ready, but did not understand why had these [:] Thank you very much!

  • 1

    @VictorAraújo https://stackoverflow.com/q/6167238

Show 12 more comments
No answers

Browser other questions tagged

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