Remove value from an array

Asked

Viewed 373 times

0

My array is mounted as follows:

obstacles = [[x, y], [x, y], ...]  

I am wanting to remove a position [x, y from the array]

But the function obstacles.remove() is turning off the server when run, if I comment on it, the rest works normally.

obstacles = []

class...

        if event[0] == 'position':
            oldPosX = self.__getattribute__('positionX')
            oldPosY = self.__getattribute__('positionY')

            obstacles.remove([int(oldPosX), int(oldPosY)])

            self.from_to(int(event[1]), int(event[2]), int(oldPosX), int(oldPosY), int(event[3]), int(event[4]), obstacles) '''função para cacular o path.'''

            if [int(event[3]), int(event[4])] not in obstacles:
                obstacles.append([int(event[3]), int(event[4])]) '''adiciona posição [x, y] no array'''
  • 1

    You don’t need to - and shouldn’t - do things like self.__getattribute__('positionX') - only self.positionX has the same effect. If by chance, you have the name "positionX" in a variable and you want to use the variable, use getattr(self, <variável>))

1 answer

1

When you have an error, always see the error message that appears on the terminal. If you don’t find out what’s going on there, when asking a question, include the error message along with your code.

In that case, you would have seen that the message is like

Valueerror: list.remove(x): x not in list

i.e.: your program stopped with an error because the element you tried to remove from the list using remove was not on the list.

The simplest way to resolve the error (although your program might have other improvements) is to test for the presence of the element before trying to remove it:

oldpos = [int(oldPosX), int(oldPosY)]
if oldpos in obstacles:
    obstacles.remove([int(oldPosX), int(oldPosY)])

That will eliminate this mistake. As to why he is not finding the set of coordinates, there may be several: but I suggest starting to see in your code if you rounded them up for integers when placing them on the list.

Another hint about code: removing list elements with "remove" can be inefficient - why Python has to go through the entire list before removing the element. If your "obstacles" don’t need to know about the order of the coordinates inside, use a set (set) instead of a list - (and use tuple and not list for the coordinate pairs inside): the time to remove an element from a set is constant.

Tip to post questions: always put the entire class and function statement with the necessary context so we can answer a question - then:

obstcales = []
class Nome(object):
   ...
   def minhafunc(self, arg1, arg2):

       ...
       if event == 'position': 
           ...

It is very difficult to answer about "position" if we do not know how it was declared, and what is inside it. If the code is small, try to put all the code. Unlike other languages where you need a lot of code, always the same, to get to the "core" where something happens, in Python the "Boiler Plate" is minimal: it helps a lot to see the whole context.

And finally, it does not cost to reperte: always, always see the error message that Python plays in the terminal when a program gives error. It never simply "to the server". (Except in rare cases where a segmentation failure occurs in a third-party module)

Browser other questions tagged

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