Remove value from an array

0

My array is mounted as follows:

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

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

But the obstacles.remove() function is shutting down the server when it is run, if I comment on it, the rest will work 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'''
    
asked by anonymous 17.09.2015 / 00:09

1 answer

0

When you have an error, always see the error message that appears on the terminal. If you do not find out what's happening, when you ask a question, include the error message along with your code.

In this case, you would have seen that the message is of type

  

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

ie your program stopped with an error because the element you tried to remove from the list using remove was not in 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 attempting to remove it:

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

This will eliminate this error. As for why he is not finding the set of coordinates, there may be several: but I suggest starting by looking at your code if you rounded them to integers by putting them on the list.

Another tip about the code: Removing list elements with "remove" may be inefficient - because Python has to go through the entire list before removing the element. If your "obstacles" do not 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 all the declaration of classes and functions with the necessary context so that 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 stated, and what is inside it. If the code is small, try putting the whole code. Unlike other languages where you need lots 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 repertate: always, always see the error message that Python throws 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)

    
17.09.2015 / 14:38