Remove function in one list alters elements in another Python3

Asked

Viewed 522 times

0

def almostIncreasingSequence(sequence):
x = sequence
for i in range(len(x)):
    sequence = x
    cont = 0
    y = False
    sequence.remove(sequence[i])
    for j in range(len(sequence)):
        try:
            if sequence[j] < sequence[j+1]:
                cont += 1
                if cont == (len(sequence)-2):
                    y = True
        except:
            if sequence[-1] > sequence[-2]:
                    cont += 1
        if cont == len(sequence)-1:
            if y == True:
                pass
            else:
                y = False
return y

I have a problem with this code, because when I use the remove in the Quence list, in addition to removing the element in the Quence list, but as it also removes the same element from the x list, I would like to know how to fix this, or if it is not possible using the remove, whether it is possible to have a variable to store the Quence list without changing it.

  • What this function needs to do?

  • Given a Sequence of integers as an array, determine whether it is possible to obtain a Strictly increasing Sequence by removing no more than one element from the array.

  • I still have the same problem, when I remove the item from the list Quence, it keeps deleting from the list x too

2 answers

3


In Python, lists are mutable, which means that when you pass a list per parameter to a function, you will not copy the object, but pass the object itself. Any modification in the object within the function, such as the remove, will be reflected in the object outside the function.

So, for example, if I pass a list of two values to a function and this removes the last element, out of the function I will also have a list of an element.

def remover_ultimo_elemento(lista):
    lista.pop()

lista = [1, 2]
remover_ultimo_elemento(lista)

print(lista)  # [1]

See working on Repl.it | Ideone

About this you can also read here:

But note that at no time does the problem statement ask to remove the element from the list; what is asked is to determine whether, from the entry list, it is possible to create a strictly increasing list by removing only one element. A list is considered strictly increasing if for any value a[n] the list is smaller than a[n+1] - realize that it is smaller, not smaller or equal.

That is, to do the check, just go through the list in pairs of values and check if the first value is less than the second. If the first is greater than or equal to the second none or once in the entire list, then the return will be true; otherwise, false.

def is_strictly_increasing(numbers):
    counter = 0
    for a, b in zip(numbers, numbers[1:]):
        if a >= b:
            counter += 1
    return counter <= 1

print(is_strictly_increasing([1, 2, 3, 4, 5]))  # True
print(is_strictly_increasing([1, 2, 3, 3, 5]))  # True
print(is_strictly_increasing([1, 2, 2, 1, 5]))  # False

Or in its abridged form:

def is_strictly_increasing(numbers):
    counter = sum(1 if a >= b else 0 for a, b in zip(numbers, numbers[1:]))
    return counter <= 1
  • Excellent solution to the problem! Since it could not be otherwise, python solves the problem with a line :D

  • Thank you very much. = D

1

In Python when you do list1 = list2 then the two lists will point to the same object and changing the value of one implies changing the value of another. You can solve your problem by making a copy of the list using list.copy().

I’d like to add that as you did x = sequence before the loop and sequence = x after the loop then even if you use the copy I suggested above, you will go back to pointing to the same object. Ex.: x = sequence.copy()

Browser other questions tagged

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