Function remove in a list changes elements in another Python3

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 remove in the sequence list, besides removing the element from the sequence list, but also removing 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, if it is possible to have a variable to save the sequence list without it being altered.

    
asked by anonymous 11.07.2018 / 01:09

2 answers

2

In Python, lists are changeable, which means that when you pass a list by parameter to a function, you will not copy the object, but rather past the object itself. Any modification to the object within the function, such as 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 in this remove the last element, outside 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 at Repl.it | Ideone

About this you can also read here:

But notice that at no time does the problem statement prompt you to remove the element from the list; you are asked to determine if from the input list you can create a strictly growing list by removing only one element. A list is considered strictly increasing if for any value a[n] of the list is less than a[n+1] - notice that it is smaller, not less than or equal.

That is, to do the verification, you simply scroll through the list in pairs of values and verify that the first value is less than the second. If the first is greater than or equal to the second none or once in the whole 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 summarized 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
    
11.07.2018 / 02:41
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 would like to add that since you did x = sequence before the loop and sequence = x after the loop so even if you use the copy I suggested above, you will return to point to the same object. Ex: x = sequence.copy()

    
11.07.2018 / 01:43