David P
David P

Reputation: 327

Python: list manipulation using del or list.remove

I am trying to write a function. This function takes a list of numbers as input, finds the largest sequence of consecutive numbers in this list and returns a list containing only this largest sequence of numbers of the original list.

Example:

In [2]: largestSeq([1,2,3,4,1,2,3])
Out[2]: [1, 2, 3, 4]

It works as long as the input list has 0 or more than 1 elements in it. I included print statements in my code to see where the error is.

Here is the code and the result of calling largestSeq([1])and largestSeq([1,2]):

Code:

def findSeq(seq):                    #this finds a sequence of consecutive numbers 
        i = 0                        #in a list and returns it
        if len(seq) <= 1:            #it stops when the next number in the list 
            return seq                #is smaller than the former
        s =[seq[0]]
        while seq[i] < seq[i+1]:
            i += 1
            s.append(seq[i])
            if i == len(seq)-1:
                break
        return s
def largestSeq(seq,a=[]):
    b = findSeq(seq)                 #find the first consecutive sequence
    if len(seq) == 0:
        return a
    print 'Length of b is ' + str(len(b))
    if len(b) > len(a):            #check if found sequence is bigger than 
        print 'seq is now ' + str(seq)#the last found sequence
        print 'b is now ' + str(b)
        i = len(b)
        print 'now deleting elements of seq'
        for d in range (i):
            seq.remove(seq[0])    #remove found sequence from the original 
        #del seq[0:i]            #list
        print 'seq is now ' + str(seq)
        print 'b is now ' + str(b)
        return largestSeq(seq,b) #start over
    else:
        del seq[0:len(b)]
        return largestSeq(seq,a)

And now the calls:

In [14]: largestSeq([1])
Length of b is 1
seq is now [1]
b is now [1]
now deleting elements of seq
seq is now []
b is now []
Out[14]: []
largestSeq([1,2])
Length of b is 2
seq is now [1, 2]
b is now [1, 2]
now deleting elements of seq
seq is now []
b is now [1, 2]
Out[15]: [1, 2]

Please note, that in the first call, the element in b is also deleted after deleting element of seq , althoug I didn't change it! In the second call with [1,2] b is behaving like I want it, while seq is deleted.

I tried manipulating the list with list.remove and with del (which is commented out and yields the same error).

What is going on in there? I don't understand it. I want b remaining unchanged in the first call, like it does in the second call.

This is a very specific question. I would be grateful for any suggestions!

Upvotes: 0

Views: 113

Answers (1)

In the first case you are returning the same list, you have to return a copy of the list.

Try:

def findSeq(seq):                    #this finds a sequence of consecutive numbers 
        i = 0                        #in a list and returns it
        if len(seq) <= 1:            #it stops when the next number in the list 
            return list(seq)                #is smaller than the former
        s =[seq[0]]
        while seq[i] < seq[i+1]:
            i += 1
            s.append(seq[i])
            if i == len(seq)-1:
                break
        return s

Upvotes: 1

Related Questions