1

I'm trying to separate every continuous segment of consecutive numbers in a different array. For example,

# Input
x=[1,2,3,4,5,8,11,12,13,18]
# Output:
x=[[1,2,3,4,5],[8],[11,12,13],[18]]

The existing code,

x=[1,2,3,4,5,8,11,12,13,18]
temp=[]

firstnumber=0
for i in range(1,len(x)-1,1):
    current=x[i]
    previous=x[i-1]
    if ((current-previous)!=1):
        mm=(x[firstnumber:i-1])
        temp.append(mm)
        firstnumber=x[i]
print(temp)


I only got [[1, 2, 3, 4], []] as a result and I can't figure out why.

ranka47
  • 995
  • 8
  • 25
user2241397
  • 69
  • 2
  • 11

4 Answers4

1

I have tried to answer this question changing as little of your code as possible.

x=[1,2,3,4,5,8,11,12,13,18]
temp=[]

firstnumber=0
first_index = 0
for i in range(1, len(x)):
    current=x[i]
    previous=x[i-1]
    if ((current-previous)!=1):
        mm = x[first_index:i]
        temp.append(mm)
        firstnumber = x[i]
        first_index = i

temp.append(x[first_index:])

print(temp)  # [[1, 2, 3, 4, 5], [8], [11, 12, 13], [18]]

What I changed: firstnumber is being used as an index, but in reality is an element of the list, so we need to use first_index = i, the current index on that iteration.

The loop did not cover all the elements of the list, we need to go all the way to the end of the list so we iterate over range(1, len(x)

Finally even if the loop completes it will be missing the last sequence unless we add it after the loop, hence the addition of temp.append(x[first_index:])

NOTE: This method will work with the input you have but it not robust for all cases, nor is it the most efficient way to do this, however, your question was why it did not work as is so hopefully this answers that.

Alex
  • 1,172
  • 11
  • 31
  • Thanks you so much, just out of curiosity, what would be the most efficient way? – user2241397 Jul 05 '19 at 18:44
  • There are a lot of different ways to do this, and depending on the constraints of the problem (is the list always sorted, etc) it's hard to say what the most efficient way is. I can provide an *alternate* way that is *more* efficient if you'd like. – Alex Jul 05 '19 at 18:50
1

My answer does not intend to provide repaired code, but rather doing described task. Note that you might use -1 index meaning last element. I would do it following way

x=[1,2,3,4,5,8,11,12,13,18]
temp=[x[:1]]
for i in x[1:]:
    if temp[-1][-1]+1!=i: temp.append([])
    temp[-1].append(i)
print(temp)

Output:

[[1, 2, 3, 4, 5], [8], [11, 12, 13], [18]]

Explanation: I firstly load first element as one-element list, then for following elements, if there is difference other than 1 between current and last-seen element then I append new empty list to temp, then independently from full-filling or not condition I add current element to last sublist.

Daweo
  • 31,313
  • 3
  • 12
  • 25
0
x=[1,2,3,4,5,8,11,12,13,18]
x.append(x[-1]-2)
temp=[]

firstnumber=0
for i in range(1, len(x)):
    current=x[i]
    previous=x[i-1]
    if ((current-previous)!=1):
        mm=(x[firstnumber:i])
        temp.append(mm)
        firstnumber=i
print(temp)
tpain
  • 144
  • 6
-1

In the code, the variable firstnumber is, I believe, supposed to contain the index of the first element of any continuous segment of consecutive numbers.

However, when you do firstnumber=x[i] that purpose is lost. Instead you can do, firstnumber = i and then it will work.

Also, this implementation will not append the last consecutive segment. As a result, you will have to do that outside the loop.

ranka47
  • 995
  • 8
  • 25